C#文件二进制流读取与保存

//保存到数据库中
using (WanWeiTiKuEntities WanweiTiku = new WanWeiTiKuEntities())
{
tk_KeChen Kecheng = new tk_KeChen();
Stream stream = new FileStream("D:\\112.doc", FileMode.Open);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
Kecheng.JuanTou = buffer;
Kecheng.KeCheng_name = "历史";
Kecheng.KeCheng_id = 0;
WanweiTiku.tk_KeChen.Add(Kecheng);
WanweiTiku.SaveChanges();
}

 

//从数据库中读取
using (WanWeiTiKuEntities WanweiTiku = new WanWeiTiKuEntities())
{
var result1 = (from u in WanweiTiku.tk_KeChen
select u).ToList<tk_KeChen>();
var result2 = from u in result1
where u.KeCheng_name == "历史"
select u;
FileStream fs = new FileStream("D:\\juantou.doc", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
foreach (var item in result2)
{
bw.Write((Byte[])item.JuanTou);
}
bw.Close();
fs.Close();
MessageBox.Show("保存成功!");

}

posted on 2014-03-20 19:44  菜鸟阿文  阅读(3279)  评论(0编辑  收藏  举报