In the programming, we often encounter such a problem that "saving files to the database", although this is not a difficult problem, but it may be difficult for some friends who have just started programming. In fact, the method is very simple, but maybe because these friends have just started programming, there is no way.
Here are the use of C # to complete this task.
First, let's introduce the saved file into the database. Save the file into the database, in fact, after converting files into binary streams, save binary streams to the corresponding field of the database. The data type of this field in SQL Server is Image, and the data type of this field in Access is an 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));
SqlConnection CN = New SQLCONNECTION ();
Cn.connectionstring = "data source = (local); user ID = sa; password =; initial catalog = test";
Sqlcommand cm = new SQLCOMMAND ();
cm.connection = cn;
CM.commandType = CommandType.Text;
IF (cn.state == 0) cn.open ();
Cm.commandtext = "INSERT INTO T_1 (FileImage) 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 ()
Filename in the code is the full name of the file, TABLENAME is the table name to be operated, and FieldName is the field name to save the file.
The two code is actually the same, but the database is different, and the objects used are different.
Then, talking about the file from the database, only is described in SQL Server. SqlDataReader Dr = NULL;
SqlConnection CN = New SQLCONNECTION ();
Cn.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 FILEIMAGE FROM T_1 WHERE ID = 1";
Cn.open ();
DR = cm.executeReader ();
Byte [] file = NULL;
IF (Dr.Read ())
{
File = (Byte []) DR [0];
}
Dr.close ();
Cn.close ();
FileStream Fs;
FileInfo Fi = new system.io.fileinfo (filename);
FS = FI.OpenWrite ();
fs.write (file, 0, file.length);
fs.close ();
The above code is to read the files saved in the database and saving the file file files specified by the file.
When using the above code, don't forget to add system.data, system.data.sqlclient and system.io reference.