从表中读取二进制数据

时间:2013-03-18 19:14:49

标签: c# sql postgresql-9.2

我有PostgreSQL 9.2,有这个表:

CREATE TABLE "Task"
(
  "taskID" serial NOT NULL,
  "taskType" text NOT NULL,
  "taskComment" text NOT NULL,
  "taskDate" date NOT NULL,
  "taskeImage" bytea,
  CONSTRAINT "Task_pkey" PRIMARY KEY ("taskID")
)

该表已由记录填充。我只想阅读taskeImage并将其显示在pictureBox1

这是我的尝试:

//some initial code goes here
NpgsqlDataReader dr = command.ExecuteReader();
dr.Read();
pictureBox1.Image=dr[4];
conn.Close();

1 个答案:

答案 0 :(得分:2)

PictureBox.Image的类型为Image,dr[4]可能会返回byte[]。您需要从byte []创建一个图像,如下所示:

using (MemoryStream ms = new MemoryStream((byte[])dr[4]))
{
    pictureBox1.Image= Image.FromStream(ms);
}