Dim cn As New SqlConnection(strCn)
cn.Open()
'Retrieve BLOB from database into DataSet.
Dim cmd As New SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "BLOBTest")
Dim c As Integer = ds.Tables("BLOBTest").Rows.Count
If c > 0 Then
'BLOB is read into Byte array, then used to construct MemoryStream,
'then passed to PictureBox.
Dim byteBLOBData As [Byte]() = New [Byte](-1) {}
byteBLOBData = DirectCast(ds.Tables("BLOBTest").Rows(c - 1)("BLOBData"), [Byte]())
Dim stmBLOBData As New MemoryStream(byteBLOBData)
pictureBox1.Image = Image.FromStream(stmBLOBData)
End If
cn.Close()
Code (C#)
SqlConnection cn = new SqlConnection(strCn);
cn.Open();
//Retrieve BLOB from database into DataSet.
SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "BLOBTest");
int c = ds.Tables["BLOBTest"].Rows.Count;
if(c>0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);
}
cn.Close();