文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > C# 图片保存到数据库和从数据库读取图片并显示
侵权投诉

C# 图片保存到数据库和从数据库读取图片并显示

C# 图片保存到数据库和从数据库读取图片并显示

C# 图片保存到数据库和从数据库读取图片并显示

图片保存到数据库的方法:

public void imgToDB(string sql)

{ //参数sql中要求保存的imge变量名称为@images

//调用方法如:imgToDB("update UserPhoto set where UserNo='" + temp + "'");

FileStream fs = File.OpenRead(t_photo.Text);

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

fs.Read(imageb, 0, imageb.Length);

fs.Close();

SqlCommand com3 = new SqlCommand (sql,con);

com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb;

if (com3.Connection.State == ConnectionState.Closed)

com3.Connection.Open();

try

{

com3.ExecuteNonQuery();

}

catch

{ }

finally

{ com3.Connection.Close(); }

}

数据库中读出图片并显示在picturebox中:

方法一:

private void ShowImage(string sql)

{

//调用方法如:ShowImage("select Photo from UserPhoto where UserNo='" + userno +"'"); SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();

byte[] b= (byte[])cmd.ExecuteScalar();

if (b.Length 〉 0)

{

MemoryStream stream = new MemoryStream(b, true);

stream.Write(b, 0, b.Length);

pictureBox1.Image = new Bitmap(stream);

stream.Close();

}

conn.Close();

}

第1页

你可能喜欢

  • SQL数据库
  • txt文件
  • C#数据库连接
  • 上传图片
  • API大全
  • 数据库课程设计实例

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email