将数据库中二进制文件,保存到本地磁盘或用网页方式下载

using System;







using System.Collections.Generic;







using System.Text;







using System.Data;







using System.Data.SqlClient;







using System.IO;















namespace ConsoleApplication2







{







class Program







{







//把文件写入数据库







public void add(string pathName)







{







FileStream fs = new FileStream(pathName, FileMode.Open, FileAccess.Read);







byte[] buffByte = new byte[fs.Length];







fs.Read(buffByte, 0, (int)fs.Length);







fs.Close();







fs = null;







SqlConnection conn = new SqlConnection(@"data source=test;uid=sa;pwd=test;database=test");







string sqlstr = @"Insert into table1(doc) values(@img)";







SqlCommand cmd = new SqlCommand();







cmd.CommandType = CommandType.Text;







cmd.CommandText = sqlstr;







cmd.Connection = conn;







cmd.Parameters.Add("@img", System.Data.SqlDbType.Image);







cmd.Parameters[0].Value = buffByte;







conn.Open();







cmd.ExecuteNonQuery();







conn.Close();







buffByte = null;















}







//从数据库读出文件







public void save(string fileName)







{







SqlConnection conn = new SqlConnection(@"data source=test;uid=sa;pwd=test;database=test");







conn.Open();







SqlCommand cmd = new SqlCommand("select top 1 doc from table1", conn);







SqlDataReader reader = cmd.ExecuteReader();







byte[] buffByte = null;







if (reader.Read())







{







buffByte = (byte[])reader[0];







}







reader.Close();







conn.Close();







FileStream fs;







FileInfo fi = new FileInfo(fileName);







fs = fi.OpenWrite();







fs.Write(buffByte, 0, buffByte.Length);







fs.Close();







}







static void Main(string[] args)







{







Program p = new Program();







p.add(@"C:/test.doc");







p.save(@"C:/test1.doc");







}







}







}







//bs保存文件:



protected void downloadFile(string strYYBH)



{



SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["LIMSConnectionString"].ToString());



conn.Open();



SqlCommand cmd = new SqlCommand("select top 1 F_file,F_fileName from FileDoc where F_YYBH='" + strYYBH.ToString().Trim() + "'", conn);



SqlDataReader reader = cmd.ExecuteReader();



byte[] buffByte = null;



string fileName = string.Empty;



if (reader.Read())



{



buffByte = (byte[])reader[0];



fileName = reader[1].ToString();



}



reader.Close();



conn.Close();







Response.ContentType = "application/octet-stream/unknow";



Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));



this.Response.Clear();



System.IO.Stream fs = this.Response.OutputStream;



fs.Write(buffByte, 0, buffByte.Length);



fs.Close();







this.Response.End();



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值