请帮助[在此代码中...插入后将数据库中的图像检索到图片框中 [英] Please Help[ in this code...Retrieve image from database to picture box after insertion
本文介绍了请帮助[在此代码中...插入后将数据库中的图像检索到图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是数据网格单元内容事件.
我可以在else语句中编写什么代码来从属于不同学生的数据库中加载特定图像?
This is data grid cell content event.
What is the code I can write in the else statement to load the particular image from database which is belonging to different students?
byte[] imgByte = null;
if (imgByte.Length == 0)
{
pictureBox1.Image = Image.FromFile("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image.jpg");
}
else
{
MemoryStream ms = new MemoryStream((Byte[])dr[15]);
//
// What is the code here to retrieve different images from the database?
}
推荐答案
假设dr [15]保留了从数据库返回的数据,将其加载到图片框很简单:
Assuming that dr[15] holds the data returned from your database, it is simple to load that into a picturebox:
MemoryStream ms = new MemoryStream((byte[]) dr[15]);
myPictureBox.Image = Image.FromStream(ms);
如果可以避免使用数字,则是个坏主意-请改用列名.它使代码更具可读性,并改善了维护:数据库可以更改(或SELECT命令可以更改)而不会影响软件的其他部分.
It is a bad idea to use numbers if you can avoid it - use the column name instead. It makes the code more readable, and also improves maintenance: your database can change (or your SELECT command can) without affecting other parts of your software.
MemoryStream ms = new MemoryStream((byte[]) dr["imageData"]);
myPictureBox.Image = Image.FromStream(ms);
这篇关于请帮助[在此代码中...插入后将数据库中的图像检索到图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文