Retrieving Image from SQLServer database
-
Hi I read a field of type Image from a SQLServer database. Now, what I want to do is creating a
System.Drawing.Image
object and holding my retrieved image into it. How?
Don't forget, that's
Persian Gulf
not Arabian gulf!
-
Hi I read a field of type Image from a SQLServer database. Now, what I want to do is creating a
System.Drawing.Image
object and holding my retrieved image into it. How?
Don't forget, that's
Persian Gulf
not Arabian gulf!
-
Hi I read a field of type Image from a SQLServer database. Now, what I want to do is creating a
System.Drawing.Image
object and holding my retrieved image into it. How?
Don't forget, that's
Persian Gulf
not Arabian gulf!
Use
SqlCommand
'sExecuteScalar
method to retrieve the blob as a byte array, and load it with theImage.FromStream(...)
static method using aMemoryStream
object. Sounds easy, eh? Well, here's a code sample.GetImage()
returns an Image object as you requested.public Image GetImage()
{
return Image.FromStream(new MemoryStream(LoadImageBLOB(mImagePK)));
}private byte[] LoadImageBLOB(int imagePK)
{
string sql = "SET TEXTSIZE 1000000\n" +
"SELECT the_blob_column FROM the_table " +
"WHERE the_where_clause\n" +
"SET TEXTSIZE 0";SqlConnection cn = new SqlConnection("_the\_connection\_string_"); SqlCommand cmd = new SqlCommand(sql, cn); cn.Open(); byte\[\] blob = (byte\[\])pq.ExecuteScalar(); cn.Close(); return blob;
}
-
Use
SqlCommand
'sExecuteScalar
method to retrieve the blob as a byte array, and load it with theImage.FromStream(...)
static method using aMemoryStream
object. Sounds easy, eh? Well, here's a code sample.GetImage()
returns an Image object as you requested.public Image GetImage()
{
return Image.FromStream(new MemoryStream(LoadImageBLOB(mImagePK)));
}private byte[] LoadImageBLOB(int imagePK)
{
string sql = "SET TEXTSIZE 1000000\n" +
"SELECT the_blob_column FROM the_table " +
"WHERE the_where_clause\n" +
"SET TEXTSIZE 0";SqlConnection cn = new SqlConnection("_the\_connection\_string_"); SqlCommand cmd = new SqlCommand(sql, cn); cn.Open(); byte\[\] blob = (byte\[\])pq.ExecuteScalar(); cn.Close(); return blob;
}
Thanx :):rose::):rose::)
Don't forget, that's
Persian Gulf
not Arabian gulf!