C # saving files to the database or reading files from the database
in programming, we often encounter the problem of" saving files to the Database, although this is no longer a difficult problem, 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.
to save a file to the database, convert the file to a binary stream and 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 ()
// Save the client file to the database
SQL = "Update t_mail set Attachfilename = @ attachfilename, attachfile = @ attachfile Where mailid = "+ mailid;
Mycommand = new sqlcommand (SQL, new sqlconnection (connstr ));
String Path = fl_name.postedfile.filename;
String filename = path. substring (path. lastindexof (" \ ") + 1, path. Length-path.LastIndexOf (" \ ")-1 );
Mycommand. Parameters. Add ("@ attachfilename", sqldbtype. varchar );
Mycommand. Parameters ["@ attachfilename"]. value = filename;
mycommand. parameters. add ("@ attachfile", sqldbtype. image);
stream filestream = fl_name.postedfile.inputstream;
int intfilesize = fl_name.postedfile.contentlength;
byte [] filecontent = new byte [intfilesize];
int intstatus = filestream. read (filecontent, 0, intfilesize); // The file is read to the filecontent array
mycommand. parameters ["@ attachfile"]. value = (byte []) filecontent);
filestream. close ();
mycommand. connection. open ();
mycommand. executenonquery ();
mycommand. connection. close ();
the filename in Code is the complete name of the file, tablename is the name of the table to be operated, and fieldname is the name of the field to save the file.
the two sections of code are actually the same, but they only use different objects for different databases.
next, let's talk about how to read files from the database and only read files 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 ()
{< br> file = (byte []) Dr [0];
}< br> filestream FS;
fileinfo Fi = new system. io. fileinfo (filename);
FS = Fi. openwrite ();
FS. write (file, 0, file. length);
FS. close ();
the code above 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.
Reference: http://www.knowsky.com/345292.html