C # files saved to the database or read from the database _ Practical Tips

Source: Internet
Author: User
Tags access database
In fact, the method is very simple, but probably because these friends just started programming soon, temporarily did not find a way.
Here's a brief introduction to using C # to accomplish this task.
First, I'll introduce you to save the file to the database.
Save the file to the database, in effect, after converting the file into a binary stream, save the binary stream to the corresponding field in the database. The data type for this field in SQL Server is image, which is the OLE object in Access.
Copy Code code as follows:

Save files to 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 a file in an 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 client files to 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); File read to filecontent array
mycommand.parameters["@attachfile"]. Value= ((byte[]) filecontent);
Filestream.close ();
MyCommand.Connection.Open ();
Mycommand.executenonquery ();
MyCommand.Connection.Close ();

FileName in code is the full name of the file, TableName is the name of the table to be manipulated, FieldName is the name of the field where you want to save the file.
The two pieces of code are actually the same, but the database of the operation is different and the objects used are different.
Then, talking about reading the file from the database, only read from SQL Server.
Copy Code code as follows:

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 file saved in the database and saves the file specified by filename.
When using the above code, don't forget to add System.Data.SqlClient and System.IO references.
Modify:
The code to read the following section of the file
Copy Code code as follows:

FileStream FS;
FileInfo fi=new System.IO.FileInfo (fileName);
Fs=fi. Openwrite ();
Fs. Write (file,0,file.length);
Fs. Close ();
Amended to
FileStream fs=new FileStream (filename,filemode.createnew);
BinaryWriter bw=new BinaryWriter (FS);
Bw. Write (file,0,file.length);
Bw. Close ();
Fs. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.