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

