C# 文件保存到數(shù)據(jù)庫中或者從數(shù)據(jù)庫中讀取文件
更新時(shí)間:2009年03月19日 00:48:04 作者:
在編程中我們常常會(huì)遇到“將文件保存到數(shù)據(jù)庫中”這樣一個(gè)問題,雖然這已不是什么高難度的問題,但對于一些剛剛開始編程的朋友來說可能是有一點(diǎn)困難。
其實(shí),方法非常的簡單,只是可能由于這些朋友剛剛開始編程不久,一時(shí)沒有找到方法而已。
下面介紹一下使用C#來完成此項(xiàng)任務(wù)。
首先,介紹一下保存文件到數(shù)據(jù)庫中。
將文件保存到數(shù)據(jù)庫中,實(shí)際上是將文件轉(zhuǎn)換成二進(jìn)制流后,將二進(jìn)制流保存到數(shù)據(jù)庫相應(yīng)的字段中。在SQL Server中該字段的數(shù)據(jù)類型是Image,在Access中該字段的數(shù)據(jù)類型是OLE對象。
//保存文件到SQL Server數(shù)據(jù)庫中
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()
//保存文件到Access數(shù)據(jù)庫中
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()
//保存客戶端文件到數(shù)據(jù)庫
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); //文件讀取到fileContent數(shù)組中
myCommand.Parameters["@attachfile"].Value=((byte[])fileContent);
fileStream.Close();
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
代碼中的fileName是文件的完整名稱,tableName是要操作的表名稱,fieldName是要保存文件的字段名稱。
兩段代碼實(shí)際上是一樣的,只是操作的數(shù)據(jù)庫不同,使用的對象不同而已。
接著,在說說將文件從數(shù)據(jù)庫中讀取出來,只介紹從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();
上面的代碼是將保存在數(shù)據(jù)庫中的文件讀取出來并保存文fileName指定的文件中。
在使用上面的代碼時(shí),別忘了添加System.Data.SqlClient和System.IO引用。
修改:
將讀文件的下面部分的代碼
FileStream fs;
FileInfo fi=new System.IO.FileInfo(fileName);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();
修改為
FileStream fs=new FileStream(fileName,FileMode.CreateNew);
BinaryWriter bw=new BinaryWriter(fs);
bw.Write(File,0,File.Length);
bw.Close();
fs.Close();
下面介紹一下使用C#來完成此項(xiàng)任務(wù)。
首先,介紹一下保存文件到數(shù)據(jù)庫中。
將文件保存到數(shù)據(jù)庫中,實(shí)際上是將文件轉(zhuǎn)換成二進(jìn)制流后,將二進(jìn)制流保存到數(shù)據(jù)庫相應(yīng)的字段中。在SQL Server中該字段的數(shù)據(jù)類型是Image,在Access中該字段的數(shù)據(jù)類型是OLE對象。
復(fù)制代碼 代碼如下:
//保存文件到SQL Server數(shù)據(jù)庫中
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()
//保存文件到Access數(shù)據(jù)庫中
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()
//保存客戶端文件到數(shù)據(jù)庫
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); //文件讀取到fileContent數(shù)組中
myCommand.Parameters["@attachfile"].Value=((byte[])fileContent);
fileStream.Close();
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
代碼中的fileName是文件的完整名稱,tableName是要操作的表名稱,fieldName是要保存文件的字段名稱。
兩段代碼實(shí)際上是一樣的,只是操作的數(shù)據(jù)庫不同,使用的對象不同而已。
接著,在說說將文件從數(shù)據(jù)庫中讀取出來,只介紹從SQL Server中讀取。
復(fù)制代碼 代碼如下:
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();
上面的代碼是將保存在數(shù)據(jù)庫中的文件讀取出來并保存文fileName指定的文件中。
在使用上面的代碼時(shí),別忘了添加System.Data.SqlClient和System.IO引用。
修改:
將讀文件的下面部分的代碼
復(fù)制代碼 代碼如下:
FileStream fs;
FileInfo fi=new System.IO.FileInfo(fileName);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();
修改為
FileStream fs=new FileStream(fileName,FileMode.CreateNew);
BinaryWriter bw=new BinaryWriter(fs);
bw.Write(File,0,File.Length);
bw.Close();
fs.Close();
相關(guān)文章
asp.net中Timer無刷新定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無刷新定時(shí)器的實(shí)現(xiàn)方法,是一個(gè)非常具有實(shí)用價(jià)值的技巧,需要用到Ajax技術(shù),需要的朋友可以參考下2014-08-08Visual Studio實(shí)現(xiàn)xml文件使用app.config、web.config等的智能提示
這篇文章主要為大家詳細(xì)介紹了Visual Studio中xml文件使用app.config、web.config等的智能提示方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09DataList 中動(dòng)態(tài)綁定服務(wù)器子控件的代碼
DataList 中動(dòng)態(tài)綁定服務(wù)器子控件的代碼...2007-09-09DataGrid同時(shí)具有分頁和排序功能及注意點(diǎn)
DataGrid同時(shí)具有分頁和排序功能及注意點(diǎn)...2006-09-09asp.net mvc4 mysql制作簡單分頁組件(部分視圖)
這篇文章主要介紹了asp.net mvc4 mysql制作簡單分頁組件,附部分視圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10利用sender的Parent獲取GridView中的當(dāng)前行(獲取gridview的值)
這篇文章主要介紹了利用sender的Parent獲取GridView中的當(dāng)前行的方法,大家參考使用吧2014-01-01