C # Save the file to the database or read the file from the database
In programming, we often encounter the problem of "saving files to the Database". Although this is not a difficult issue, but it may be a little difficult for some friends who have just started programming. In fact, the method is very simple, but it may be because these friends have not found a method at the moment since they just started programming.
The following describes how to use C # to complete this task.
First, we will introduce how to save files to the database.
Save the file to the database. In fact, after converting the file into a binary stream, save the binary stream to the corresponding fields of the database. In SQL Server, the Data Type of this field is image, and in access, the Data Type of this field is OLE object.
// Save the file to the SQL Server database
Fileinfo Fi = new fileinfo (filename );
Filestream FS = Fi. openread ();
Byte [] bytes = new byte [fs. Length];
FS. Read (bytes, 0, convert. toint32 (FS. Length ));
Sqlcommand CM = new sqlcommand ();
Cm. Connection = cn;
Cm. commandtype = commandtype. text;
If (CN. State = 0) Cn. open ();
Cm. commandtext = "insert into" + tablename + "(" + fieldname + ") values (@ file )";
Sqlparameter spfile = new sqlparameter ("@ file", sqldbtype. Image );
Spfile. value = bytes;
Cm. Parameters. Add (spfile );
Cm. executenonquery ()
// Save the file to the Access Database
Fileinfo Fi = new fileinfo (filename );
Filestream FS = Fi. openread ();
Byte [] bytes = new byte [fs. Length];
FS. Read (bytes, 0, convert. toint32 (FS. Length ));
Oledbcommand CM = new oledbcommand ();
Cm. Connection = cn;
Cm. commandtype = commandtype. text;
If (CN. State = 0) Cn. open ();
Cm. commandtext = "insert into" + tablename + "(" + fieldname + ") values (@ file )";
Oledbparameter spfile = new oledbparameter ("@ file", oledbtype. Binary );
Spfile. value = bytes;
Cm. Parameters. Add (spfile );
Cm. executenonquery ()
In the code, filename is the complete name of the file, tablename is the name of the table to be operated, and fieldname is the field name of the file to be saved.
The two pieces of code are actually the same, but they are different in operating databases and using different objects.
Next, let's talk about reading files from the database and only reading from SQL Server.
Sqldatareader DR = NULL;
Sqlconnection objcn = new sqlconnection ();
Objcn. connectionstring = "Data Source = (local); User ID = sa; Password =; initial catalog = test ";
Sqlcommand CM = new sqlcommand ();
Cm. Connection = cn;
Cm. commandtype = commandtype. text;
Cm. commandtext = "select" + fieldname + "from" + tablename + "where id = 1 ";
Dr = cm. executereader ();
Byte [] file = NULL;
If (dr. Read ())
{
File = (byte []) Dr [0];
}
Filestream FS;
Fileinfo Fi = new system. Io. fileinfo (filename );
FS = Fi. openwrite ();
FS. Write (file, 0, file. Length );
FS. Close ();
The above Code reads the files stored in the database and saves the Files specified by filename.
When using the above Code, do not forget to add system. Data. sqlclient and system. Io references.
Modify:
Read the following code of the file.
Filestream FS;
Fileinfo Fi = new system. Io. fileinfo (filename );
FS = Fi. openwrite ();
FS. Write (file, 0, file. Length );
FS. Close ();
Change
Filestream FS = new filestream (filename, filemode. createnew );
Binarywriter BW = new binarywriter (FS );
Bw. Write (file, 0, file. Length );
Bw. Close ();
FS. Close ();
After this modification, you can solve the problem that "If you want to retrieve from the database and save it as a corresponding file. For example, the Word file is saved as XXX. DOC ('xxx' is the file name.