日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C# 文件保存到数据库中或者从数据库中读取文件

發布時間:2025/7/14 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 文件保存到数据库中或者从数据库中读取文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,介紹一下保存文件到數據庫中。
將文件保存到數據庫中,實際上是將文件轉換成二進制流后,將二進制流保存到數據庫相應的字段中。在SQL Server中該字段的數據類型是Image,在Access中該字段的數據類型是OLE對象。
[復制此代碼]CODE:
//保存文件到SQL Server數據庫中
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數據庫中
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()
//保存客戶端文件到數據庫
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數組中
myCommand.Parameters["@attachfile"].Value=((byte[])fileContent);
fileStream.Close();
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
代碼中的fileName是文件的完整名稱,tableName是要操作的表名稱,fieldName是要保存文件的字段名稱。
兩段代碼實際上是一樣的,只是操作的數據庫不同,使用的對象不同而已。
接著,在說說將文件從數據庫中讀取出來,只介紹從SQL Server中讀取。
[復制此代碼]CODE:
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();
上面的代碼是將保存在數據庫中的文件讀取出來并保存文fileName指定的文件中。
在使用上面的代碼時,別忘了添加System.Data.SqlClient和System.IO引用。
修改:
將讀文件的下面部分的代碼
[復制此代碼]CODE:
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();?

轉載于:https://www.cnblogs.com/colder/archive/2012/07/06/2579047.html

總結

以上是生活随笔為你收集整理的C# 文件保存到数据库中或者从数据库中读取文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。