C# 文件保存到數(shù)據(jù)庫(kù)中或者從數(shù)據(jù)庫(kù)中讀取文件
更新時(shí)間:2009年03月19日 00:48:04 作者:
在編程中我們常常會(huì)遇到“將文件保存到數(shù)據(jù)庫(kù)中”這樣一個(gè)問(wèn)題,雖然這已不是什么高難度的問(wèn)題,但對(duì)于一些剛剛開(kāi)始編程的朋友來(lái)說(shuō)可能是有一點(diǎn)困難。
其實(shí),方法非常的簡(jiǎn)單,只是可能由于這些朋友剛剛開(kāi)始編程不久,一時(shí)沒(méi)有找到方法而已。
下面介紹一下使用C#來(lái)完成此項(xiàng)任務(wù)。
首先,介紹一下保存文件到數(shù)據(jù)庫(kù)中。
將文件保存到數(shù)據(jù)庫(kù)中,實(shí)際上是將文件轉(zhuǎn)換成二進(jìn)制流后,將二進(jìn)制流保存到數(shù)據(jù)庫(kù)相應(yīng)的字段中。在SQL Server中該字段的數(shù)據(jù)類型是Image,在Access中該字段的數(shù)據(jù)類型是OLE對(duì)象。
//保存文件到SQL Server數(shù)據(jù)庫(kù)中
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ù)庫(kù)中
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ù)庫(kù)
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ù)庫(kù)不同,使用的對(duì)象不同而已。
接著,在說(shuō)說(shuō)將文件從數(shù)據(jù)庫(kù)中讀取出來(lái),只介紹從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ù)庫(kù)中的文件讀取出來(lái)并保存文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#來(lái)完成此項(xiàng)任務(wù)。
首先,介紹一下保存文件到數(shù)據(jù)庫(kù)中。
將文件保存到數(shù)據(jù)庫(kù)中,實(shí)際上是將文件轉(zhuǎn)換成二進(jìn)制流后,將二進(jìn)制流保存到數(shù)據(jù)庫(kù)相應(yīng)的字段中。在SQL Server中該字段的數(shù)據(jù)類型是Image,在Access中該字段的數(shù)據(jù)類型是OLE對(duì)象。
復(fù)制代碼 代碼如下:
//保存文件到SQL Server數(shù)據(jù)庫(kù)中
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ù)庫(kù)中
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ù)庫(kù)
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ù)庫(kù)不同,使用的對(duì)象不同而已。
接著,在說(shuō)說(shuō)將文件從數(shù)據(jù)庫(kù)中讀取出來(lái),只介紹從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ù)庫(kù)中的文件讀取出來(lái)并保存文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 數(shù)據(jù)訪問(wèn)層 存儲(chǔ)過(guò)程分頁(yè)語(yǔ)句
在asp.net 網(wǎng)頁(yè)中如果在業(yè)務(wù)邏輯層分頁(yè)在使用PagedDataSource對(duì)象,但如果數(shù)據(jù)記錄過(guò)多,使用它會(huì)嚴(yán)重的損害應(yīng)用程序的性能.2009-12-12
ASP.NET數(shù)據(jù)綁定之Repeater控件
這篇文章主要介紹了ASP.NET數(shù)據(jù)綁定中的Repeater控件,Repeater控件可以將數(shù)據(jù)庫(kù)中的信息加以綁定然后再在瀏覽器中顯示出來(lái),感興趣的小伙伴們可以參考一下2016-01-01
ASP.NET AJAX 4.0的模版編程(Template Programming)介紹
不過(guò)當(dāng)我評(píng)估ASP.NET AJAX 4.0的時(shí)候,我確實(shí)被它的特征給震住了。新的特征完全專注于瀏覽器技術(shù),比如XHTML和javascript。 我非常欽佩ASP.NET AJAX小組。2009-07-07
ASP.NET頁(yè)面間數(shù)據(jù)傳遞的幾種方法介紹
在ASP.NET中,頁(yè)面間數(shù)據(jù)傳遞的方法有很多。下面為大家總結(jié)一下,頁(yè)面間數(shù)據(jù)傳遞的方法,來(lái)看作者的分析。2013-05-05
ASP.NET MVC圖片上傳前預(yù)覽簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET MVC圖片上傳前預(yù)覽簡(jiǎn)單實(shí)現(xiàn)代碼,可以獲取圖片文件名和圖片字節(jié)大小,感興趣的小伙伴們可以參考一下2016-05-05
C# 文件保存到數(shù)據(jù)庫(kù)中或者從數(shù)據(jù)庫(kù)中讀取文件
在編程中我們常常會(huì)遇到“將文件保存到數(shù)據(jù)庫(kù)中”這樣一個(gè)問(wèn)題,雖然這已不是什么高難度的問(wèn)題,但對(duì)于一些剛剛開(kāi)始編程的朋友來(lái)說(shuō)可能是有一點(diǎn)困難。2009-03-03
Asp.Net2.0權(quán)限樹(shù)中Checkbox的操作
Asp.Net2.0權(quán)限樹(shù)中Checkbox的操作...2006-09-09
ASP.NETWeb服務(wù)器驗(yàn)證控件如何使用
這篇文章主要介紹了ASP.NETWeb服務(wù)器驗(yàn)證控件如何使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09

