使用C#在數據庫中存取文件
將文件保存到數據庫中的操作,一般有以下幾個步驟:
1.把文件轉化為字節數組
2.將字節數組存入數據庫
3.從數據庫中讀出字節數組
4.將字節數組轉化成相應的文件等,並作進一步的操作
1.把文件轉化為字節數組
//
將文件轉化為二进制流byte[]
private byte [] FileToStream( string fileName)
{
FileInfo fi = new FileInfo(fileName);
FileStream fs = fi.OpenRead();
byte [] bytes = new byte [fs.Length];
fs.Read(bytes, 0 ,Convert.ToInt32(fs.Length));
return bytes;
}
private byte [] FileToStream( string fileName)
{
FileInfo fi = new FileInfo(fileName);
FileStream fs = fi.OpenRead();
byte [] bytes = new byte [fs.Length];
fs.Read(bytes, 0 ,Convert.ToInt32(fs.Length));
return bytes;
}
//
把图片文件转化为二进制流byte[]
private byte [] ImageToStream( string fileName)
{
Bitmap image = new Bitmap(fileName);
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
return stream.ToArray();
}
private byte [] ImageToStream( string fileName)
{
Bitmap image = new Bitmap(fileName);
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
return stream.ToArray();
}
2.將字節數組byte[]存入數據庫
private
void
StoreImage(
byte
[] byt)
{
string str_Conn = " server=SERVER-DBT;database=LogERP;uid=logerp;pwd=logerpok; " ;
SqlConnection sqlconn = new SqlConnection(str_Conn);
SqlCommand cmd = new SqlCommand( " Insert into ENTR_Image(EImage) values (@EImage) " );
cmd.Connection = sqlconn;
SqlParameter imageParameter = cmd.Parameters.Add( " @EImage " , SqlDbType.Binary);
imageParameter.Value = byt;
imageParameter.Size = byt.Length;
sqlconn.Open();
cmd.ExecuteNonQuery();
sqlconn.Close();
}
{
string str_Conn = " server=SERVER-DBT;database=LogERP;uid=logerp;pwd=logerpok; " ;
SqlConnection sqlconn = new SqlConnection(str_Conn);
SqlCommand cmd = new SqlCommand( " Insert into ENTR_Image(EImage) values (@EImage) " );
cmd.Connection = sqlconn;
SqlParameter imageParameter = cmd.Parameters.Add( " @EImage " , SqlDbType.Binary);
imageParameter.Value = byt;
imageParameter.Size = byt.Length;
sqlconn.Open();
cmd.ExecuteNonQuery();
sqlconn.Close();
}
3.從數據庫中讀出字節數組
public
static
byte
[] GetByteImage(
string
str_Sql)
{
string connectionString = System.Configuration.ConfigurationSettings.AppSettings[ " ConnStr " ].ToString();
byte [] content = {};
SqlConnection sqlconn = new SqlConnection(connectionString);
sqlconn.Open();
SqlCommand cmd = new SqlCommand(str_Sql);
cmd.Connection = sqlconn;
content = ( byte [] )cmd.ExecuteScalar();
sqlconn.Close();
return content;
}
{
string connectionString = System.Configuration.ConfigurationSettings.AppSettings[ " ConnStr " ].ToString();
byte [] content = {};
SqlConnection sqlconn = new SqlConnection(connectionString);
sqlconn.Open();
SqlCommand cmd = new SqlCommand(str_Sql);
cmd.Connection = sqlconn;
content = ( byte [] )cmd.ExecuteScalar();
sqlconn.Close();
return content;
}
4.將字節數組轉化成相應的文件等,並作進一步的操作
//
根据2进制数组获得文件並保存
private void GetFileFromDataBase( byte [] byt, string str_Filename)
{
FileStream fs_stream = new FileStream(str_Filename,FileMode.CreateNew);
BinaryWriter writefile = new BinaryWriter(fs_stream);
writefile.Write(byt);
writefile.Close();
}
private void GetFileFromDataBase( byte [] byt, string str_Filename)
{
FileStream fs_stream = new FileStream(str_Filename,FileMode.CreateNew);
BinaryWriter writefile = new BinaryWriter(fs_stream);
writefile.Write(byt);
writefile.Close();
}
//
根据2进制数组获得圖片
private Image GetImage( byte [] byt)
{
MemoryStream stream = new MemoryStream(byt);
return Image.FromStream(stream);
}
private Image GetImage( byte [] byt)
{
MemoryStream stream = new MemoryStream(byt);
return Image.FromStream(stream);
}
參考文獻: 1. http://www.cnblogs.com/jhtchina/archive/2006/03/03/341850.html
本文介绍如何使用C#将文件转换为字节流并存入数据库的方法,包括从数据库读取字节流还原为文件的过程。
1577

被折叠的 条评论
为什么被折叠?



