从Access数据库中检索图像(长二进制数据)并在c#中的gridview中显示

时间:2016-03-30 05:43:14

标签: c#

我正在尝试从访问数据库中检索图像,该数据库保存为二进制长数据。我想在datagridview中显示图像以及其他数据。

image in 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();
                }

1 个答案:

答案 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));
}