C#把图片存入到SQL2008数据库中
来源:互联网 发布:淘宝确认收货多久到账 编辑:程序博客网 时间:2024/05/01 18:39
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
SqlConnection con = new SqlConnection(conmmsy);
con.Open();
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;//文件路径
//创建文件流,path参数是文件路径
FileStream fs = new FileStream(path, FileMode.Open);
int streamLength = (int)fs.Length; //获取文件流的长度。
byte[] image = new byte[streamLength]; //声明字节数组,用于保存图片文件
fs.Read(image, 0, streamLength); //把图片文件转换成为字节数组保存
fs.Close();
//插入Sql语句,@img是Sql语句参数。
string sql = string.Format("insert imagetable values(@img)", image);
SqlCommand com = new SqlCommand(sql, con); //con是一个有效的连接对象
//为命令对象添加参数,注意参数的类型
com.Parameters.Add(new SqlParameter("img", SqlDbType.Binary, image.Length,
ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));
com.ExecuteNonQuery(); //执行
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "插入图片不成功");
}
finally
{
con.Close();
}
SqlConnection con = new SqlConnection(conmmsy);
con.Open();
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;//文件路径
//创建文件流,path参数是文件路径
FileStream fs = new FileStream(path, FileMode.Open);
int streamLength = (int)fs.Length; //获取文件流的长度。
byte[] image = new byte[streamLength]; //声明字节数组,用于保存图片文件
fs.Read(image, 0, streamLength); //把图片文件转换成为字节数组保存
fs.Close();
//插入Sql语句,@img是Sql语句参数。
string sql = string.Format("insert imagetable values(@img)", image);
SqlCommand com = new SqlCommand(sql, con); //con是一个有效的连接对象
//为命令对象添加参数,注意参数的类型
com.Parameters.Add(new SqlParameter("img", SqlDbType.Binary, image.Length,
ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));
com.ExecuteNonQuery(); //执行
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "插入图片不成功");
}
finally
{
con.Close();
}
0 0
- C#把图片存入到SQL2008数据库中
- 如何把图片存入到数据库
- 如何把图片存入到数据库
- C# 把图片保存到数据库中
- C#中图片存入SQL数据库
- C# 将图片存入数据库中
- C#图片存入数据库
- WinForm把图片以二进制的方式存入到数据库,并将存入数据库的二进制图片读出显示到PictureBox中
- 把图片存入mysql数据库
- delphi 把图片存入数据库
- 把图片文件存入数据库
- 把图片文件存入数据库
- 如何在ASP.Net 中把图片存入数据库
- 如何在ASP.Net 中把图片存入数据库
- 如何在ASP.Net 中把图片存入数据库
- 如何在ASP.Net 中把图片存入数据库
- 如何在ASP.Net 中把图片存入数据库
- 如何在ASP.Net 中把图片存入数据库
- 【容器云】从运维的角度看微服务和容器
- 全球域名后缀注册量排行榜!
- C#winform如何最小化主窗口
- Ajax异步回调函数(callback)
- windows窗口分析,父窗口,子窗口,所有者窗口
- C#把图片存入到SQL2008数据库中
- 原声搜索分页删除
- 集合之LinkedList案例——生成扑克牌、逆序排序
- MYSQL 启动错误193
- 如何写出安全的API接口?接口参数加密签名设计思路
- 数论:整数规则
- Linux有关环境变量的配置示例
- jQuery的on()
- VS2012加载失败