Home > Back-end >  SQL 2000 with INSERT INTO INSERT to the specified file field, statement how to write
SQL 2000 with INSERT INTO INSERT to the specified file field, statement how to write

Time:10-04

A. (id, name, wj) three fields, such as want to insert (1, "wang wei, c: \ y.r ar) (note that the file itself is, not a filename characters) to the records, statements should be how to write?

CodePudding user response:

TXT or rar file format

CodePudding user response:

Is rar file format

CodePudding user response:

Using parameter, if it's ADO

ADOQuery - & gt; SQL - & gt; Add (" INSERT INTO a (id, name, wj) ");
ADOQuery - & gt; SQL - & gt; Add (" VALUES (1, "wang wei, : V_FILE)");
ADOQuery - & gt; The Parameters - & gt; ParamByName (" V_FILE ") - & gt; LoadFromFile (" c: \ \ y.r ar, "ftBlob);
ADOQuery - & gt; ExecSQL ();

CodePudding user response:

This besides parameter method to load the file directly, I used to use flow to upload a file, the example code is as follows:

 void __fastcall TForm1: : Button1Click (TObject * Sender) 
{//in the
AnsiString tPath;
OpenDialog1 - & gt; The Filter="Word document (*.doc) | *. Doc | Microsoft Excel workbook (*. XLS) | *. XLS | Web pages (*. HTM; | * *. HTML). HTM; *. All files HTML | | *. * ";//show the types of files
If (OpenDialog1 - & gt; The Execute ())//open file dialog box
TPath=OpenDialog1 - & gt; The FileName.//read the documents to the Memo controls
The else return;

TMemoryStream * sm=new TMemoryStream;
Sm - & gt; LoadFromFile (tPath);//origin

ADOQuery1 - & gt; Close ();
ADOQuery1 - & gt; SQL - & gt; The Clear ();
ADOQuery1 - & gt; SQL - & gt; Add (" INSERT INTO b1 (l1 and l2) VALUES (' 123 ', : l2);" );
ADOQuery1 - & gt; The Parameters - & gt; ParamByName (" l2 ") - & gt; LoadFromStream (sm, ftBlob);
ADOQuery1 - & gt; ExecSQL ();

The delete sm;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Void __fastcall TForm1: : Button2Click (TObject * Sender)
{//remove
ADOQuery1 - & gt; Close ();
ADOQuery1 - & gt; SQL - & gt; The Clear ();
ADOQuery1 - & gt; SQL - & gt; Add (" SELECT * FROM b1;" );
ADOQuery1 - & gt; The Open ();
If (ADOQuery1 - & gt; Eof) return;//return

TStream * Stream1;
Stream1=ADOQuery1 - & gt; CreateBlobStream (ADOQuery1 - & gt; FieldByName (" l2 "), bmRead);
TMemoryStream * sm=new TMemoryStream;
Sm - & gt; LoadFromStream (Stream1);
Sm - & gt; SaveToFile (" C: \ \ 1. XLS ");

The delete sm;
The delete Stream1;
}


This example has a obvious problems is the flow without reuse, in a row, a large number of uploaded files will cause problems, the solution is to move the flow of creation and destruction to the button event outside

Have a better suggestion is, SQLServer2000 according to my project experience, the data file is more than 2 t there is no problem, but for large files or large file uploads, if directly to upload the entire file into a field will lead to some problems, can use flow partition to upload,

CodePudding user response:

The final version:

 TMemoryStream * K_MLEG; 
Void __fastcall TForm1: : FormCreate (TObject * Sender)
{
K_MLEG=new TMemoryStream ();
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : FormClose (TObject * Sender, TCloseAction & amp; The Action)
{
K_MLEG - & gt; The Clear ();
The delete K_MLEG;
K_MLEG=NULL;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{//save the file to the database
OpenDialog1 - & gt; The Filter=L "Word document (*.doc) | *. Doc | Microsoft Excel workbook (*. XLS) | *. XLS | Web pages (*. HTM; | * *. HTML). HTM; *. All files HTML | | *. * ";//show the types of files
If (OpenDialog1 - & gt; The Execute ()!=true) {//open file dialog box
return;
}
String Path=OpenDialog1 - & gt; The FileName.//read the documents to the Memo controls

K_MLEG - & gt; The Clear ();//use before empty
K_MLEG - & gt; LoadFromFile (Path);//origin

ADOQuery1 - & gt; Close ();
ADOQuery1 - & gt; SQL - & gt; The Clear ();
ADOQuery1 - & gt; SQL - & gt; Add (L "INSERT INTO b1 (l1 and l2) VALUES (' 123 ', : l2);" );
ADOQuery1 - & gt; The Parameters - & gt; ParamByName "l2" (L) - & gt; LoadFromStream (K_MLEG ftBlob);
ADOQuery1 - & gt; ExecSQL ();
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : Button2Click (TObject * Sender)
{//from the database to download
ADOQuery1 - & gt; Close ();
ADOQuery1 - & gt; SQL - & gt; The Clear ();
ADOQuery1 - & gt; SQL - & gt; Add (L "SELECT * FROM b1;" );
ADOQuery1 - & gt; The Open ();
If (ADOQuery1 - & gt; Eof) {
return;//return
}

TStream * Stream1;
Stream1=ADOQuery1 - & gt; CreateBlobStream (ADOQuery1 - & gt; FieldByName "l2" (L), bmRead);

K_MLEG - & gt; The Clear ();//use before empty
K_MLEG - & gt; LoadFromStream (Stream1);
K_MLEG - & gt; SaveToFile (L "C: \ \ 1. XLS");

The delete Stream1;
}

CodePudding user response:

reference 5 floor KFRGHT reply:
final version:

 TMemoryStream * K_MLEG; 
Void __fastcall TForm1: : FormCreate (TObject * Sender)
{
K_MLEG=new TMemoryStream ();
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : FormClose (TObject * Sender, TCloseAction & amp; The Action)
{
K_MLEG - & gt; The Clear ();
The delete K_MLEG;
K_MLEG=NULL;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{//save the file to the database
OpenDialog1 - & gt; The Filter=L "Word document (*.doc) | *. Doc | Microsoft Excel workbook (*. XLS) | *. XLS | Web pages (*. HTM; | * *. HTML). HTM; *. All files HTML | | *. * ";//show the types of files
If (OpenDialog1 - & gt; The Execute ()!=true) {//open file dialog box
return;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related