博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片内容保存到数据库,并从数据库里获取图片
阅读量:6985 次
发布时间:2019-06-27

本文共 1543 字,大约阅读时间需要 5 分钟。

 

保存image到database

public bool SaveImage(string filePath)

{
bool isSuccess = false;
string FilePath = filePath;
string filename = FilePath.Substring(FilePath.LastIndexOf("\\") + 1); //得到上传文件的文名
string filetext = string.Empty;
FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] fileByte = br.ReadBytes((int)fs.Length);//将流读入到字节数组中
//Byte[] fileByte = new Byte[(int)fs.Length];
//fs.Read(fileByte, 0, fileByte.Length);
//filetext = System.Text.Encoding.Default.GetString(fileByte); //将指定字节数组中的说有字节解码为一个字符串
SqlConnection conn = new SqlConnection(@"server=.;database=SoyErp2.0;uid=sa;pwd=sa;");

conn.Open();

StringBuilder strSql = new StringBuilder();
strSql.Append("INSERT INTO dbo.picSaveToSql ( ImgFile) VALUES ( @Photo )");
SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = fileByte;
isSuccess =cmd.ExecuteNonQuery()>0?true:false;
conn.Close();
fs.Close();
return isSuccess;
}

//从database获取图片

public Bitmap Get_Image()

{
byte[] imagebytes = null;
SqlConnection conn = new SqlConnection(@"server=.;database=SoyErp2.0;uid=sa;pwd=sa;");
conn.Open();
SqlCommand com = new SqlCommand(" SELECT * FROM picSaveToSql WHERE Id=4 ", conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(1);
}
dr.Close();
conn.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
return bmpt;
}

转载于:https://www.cnblogs.com/ChineseMoonGod/p/3701518.html

你可能感兴趣的文章
Function类型
查看>>
Python学习
查看>>
ES6之let和const
查看>>
不用软件,手动修复双系统引导进win7,xp的多种方法
查看>>
python 访问需要HTTP Basic Authentication认证的资源
查看>>
java中比较字符串的大小用String的compareTo()
查看>>
plist使用
查看>>
Linux RAR 安装和使用
查看>>
【OC】【一秒就会】【collectionView 头部吸住功能】
查看>>
51CTO下载 好资料分享
查看>>
linux 下转换UTC到本地时间
查看>>
Linux的起源与各发行版的基本知识
查看>>
单播包、广播包、组播包、洪泛包
查看>>
iptables命令结构之命令
查看>>
RabbitMQ之Exchange分类
查看>>
综合布线系统的构成
查看>>
计算机硬件 — 计算机简介
查看>>
关于重写session实现的时候可能会导至nginx 502的问题
查看>>
7z(p7zip)压缩软件在Linux下的安装和使用
查看>>
jetbrick-template 1.1.0 发布,支持 #tag, #macro, layout
查看>>