使用C#在數據庫中存取文件

本文介绍如何使用C#将文件转换为字节流并存入数据库的方法,包括从数据库读取字节流还原为文件的过程。

使用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;
}

 

        // 把图片文件转化为二进制流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();
        }

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();
        } 

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;
        }

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();            
        }

 

//  根据2进制数组获得圖片
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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值