如何从数据库中读取图片到picturebox中

如何从数据库中读取图片到picturebox中... 如何从数据库中读取图片到picturebox中 展开
 我来答
th79d
2011-05-03 · TA获得超过265个赞
知道小有建树答主
回答量:203
采纳率:0%
帮助的人:252万
展开全部
用SqlDataReader读取图片数据,放到流中,Image对象从流加载数据到PictureBox。有三种方式读取图片,这三种方式都要求将SqlDataReader的默认行为设置为SequentialAccess。
1使用GetSqlBytes检索varbinary(max)数据:
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
Stream s = new MemoryStream(); //创建一个以内存为后备存储的流
SqlCommand command = connection.CreateCommand();
SqlDataReader reader = null;
try
{
command.CommandText = "SELECT LastName,Photo FROM dbo.Employees " +
" WHERE LastName=@LastName";
command.CommandType = CommandType.Text;
//声明参数并赋值
SqlParameter parameter = new SqlParameter("@LastName", SqlDbType.NVarChar, 20);
parameter.Value = lastName;
command.Parameters.Add(parameter);
connection.Open();
//修改DataReader的默认行为,SequentialAccess按顺序接收数据并立即加载
//CloseConnection指明关闭DataReader时,对数据库的连接也关闭
reader = command.ExecuteReader(
CommandBehavior.SequentialAccess|CommandBehavior.CloseConnection);
if (reader.HasRows)
{
while (reader.Read())
{
//SequentialAccess要求按顺序接收数据,先接受reader[0]
this.label1.Text = reader[0].ToString();
if (reader.IsDBNull(1)) //若列值为空返回
return;
else
{
//使用reader.GetSqlBytes获取图像数据
SqlBytes bytes = reader.GetSqlBytes(1);
using (Bitmap productImage = new Bitmap(bytes.Stream))
{
//以gif格式保存在Stream流并显示
productImage.Save(s, System.Drawing.Imaging.ImageFormat.Gif);
this.pictureBox1.Image = System.Drawing.Image.FromStream(s);
} } }
}
else
MessageBox.Show("No records returned.");
2使用GetSqlBinary检索数据:
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
SqlBinary binaryStream = reader.GetSqlBinary(0);
3使用GetValue检索数据:
while (reader.Read())
{
//如果从 varbinary(max) 列读数据
byte[] binaryData = (byte[])reader.GetValue(0);

//如果从 varchar(max)或nvarchar(max) 列读数据
String stringData = (String)reader.GetValue(1);
}
详细代码见我的“王一博客”danyaody
百度网友255d1c35d
2011-05-03 · TA获得超过135个赞
知道小有建树答主
回答量:202
采纳率:0%
帮助的人:148万
展开全部
如果是图片转换为二进制存储在数据库里面,就将数据库里面的二进制转换为图片,之后保存到指向picturebox的image
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
14...5@qq.com
2011-05-03 · TA获得超过1008个赞
知道小有建树答主
回答量:1369
采纳率:60%
帮助的人:815万
展开全部
哪种语言?如果是c#的话,用这个
this.pictureBox1.Image = Image.FromFile("d:\\1.jpg", false);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式