Нужно из базы MSSQLSERVER2000 вычитать поле типа Image в переменную типа Image - C#
Формулировка задачи:
На C# из базы MSSQLSERVER2000
вычитать поле типа Image
в переменную типа Image
пишу так
string mySelectQuery = 'SELECT image from colors where id=2';
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
byte[] b= new byte[0];
while (myReader.Read())
{
b=(byte[])myReader.GetSqlBinary(0).Value;
}
MemoryStream ms = new MemoryStream(b,0,b.Length);
ms.Seek(0, SeekOrigin.Begin);
// массив получил
im = Bitmap.FromStream(ms); // вылетает здесьРешение задачи: «Нужно из базы MSSQLSERVER2000 вычитать поле типа Image в переменную типа Image»
textual
Листинг программы
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows)
{
myReader.Read();
byte[] b = myReader.GetSqlBinary(0).Value;
MemoryStream ms = new MemoryStream(b,0,b.Length);
Bitmap im = (Bitmap)Bitmap.FromStream(ms);
}