我正在尝试从访问数据库中检索图像,该数据库保存为二进制长数据。我想在datagridview中显示图像以及其他数据。
String sql = "SELECT Config.ConfigID, Config.ProductName, Config.Features, Config.Price, Config.Picture, Stock.Quantity, Stock.TotalPrice FROM Config INNER JOIN Stock ON Config.ConfigID = Stock.ConfigID";
cmd = new OleDbCommand(sql, con);
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dataGridView1.Rows.Clear();
try
{
while (rdr.Read() == true)
{
dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6]);
}
con.Close();
}
答案 0 :(得分:1)
试试这个:
while (rdr.Read() == true)
{
string path = @"c:\mytest.bmp";
int pictureCol = 4; // the column #
Byte[] b = new Byte[(rdr.GetBytes(pictureCol, 0, null, 0, int.MaxValue))];
rdr.GetBytes(pictureCol, 0, b, 0, b.Length);
using(System.IO.FileStream fs = new System.IO.FileStream(path,
System.IO.FileMode.Create,System.IO.FileAccess.Write))
{
fs.Write(b, 0, b.Length);
}
dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6],
Bitmap.FromFile(path));
}