C# 如何从数据库中读取图像
每个人我都写了一些代码来读取C#net中数据库中的图像,但是我无法知道这里发生错误的地方。这是我的密码C# 如何从数据库中读取图像,c#,database,C#,Database,每个人我都写了一些代码来读取C#net中数据库中的图像,但是我无法知道这里发生错误的地方。这是我的密码 public class Images { string imageFilename = null; byte[] imageBytes = null; SqlConnection imageConnection = null; SqlCommand imageCommand = null; SqlDataReader imageReader = null;
public class Images
{
string imageFilename = null;
byte[] imageBytes = null;
SqlConnection imageConnection = null;
SqlCommand imageCommand = null;
SqlDataReader imageReader = null;
public Images()
{
imageConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
imageCommand = new SqlCommand(@"select imagefile, imagedata from imagetable", imageConnection);
imageConnection.Open();
imageReader = imageCommand.ExecuteReader();
}
public Bitmap GetImage()
{
MemoryStream ms = new MemoryStream(imageBytes);
Bitmap bmap = new Bitmap(ms);
return bmap;
}
public string GetFilename()
{
return imageFilename;
}
public bool GetRow()
{
if (imageReader.Read())
{
imageFilename = (string) imageReader.GetValue(0);
imageBytes = (byte[]) imageReader.GetValue(1);
}
else
{
}
}
public void EndImages()
{
imageReader.Close();
imageConnection.Close();
}
如果在调用GetRow()之前调用GetImage(),将会出现错误。此外,位图类无法跨AppDomains访问(例如,在创建它的域以外的域中调用DrawImage())阅读此链接可能对您有所帮助:请告诉我们错误是什么。
public class Images
{
string imageFilename = null;
byte[] imageBytes = null;
SqlConnection imageConnection = null;
SqlCommand imageCommand = null;
SqlDataReader imageReader = null;
public Images()
{
imageConnection = new SqlConnection("server= (local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
imageCommand = new SqlCommand(@"select imagefile, imagedata from imagetable", imageConnection);
imageConnection.Open();
imageReader = imageCommand.ExecuteReader(); }
public Bitmap GetImage()
{
MemoryStream ms = new MemoryStream(imageBytes);
Bitmap bmap = new Bitmap(ms);
return bmap;
}
public string GetFilename()
{
return imageFilename;
}
public bool GetRow()
{
if (imageReader.Read())
{
imageFilename = (string) imageReader.GetValue(0);
imageBytes = (byte[]) imageReader.GetValue(1);
return true;
}
else
{
return false;
}
}
public void EndImages()
{
imageReader.Close();
imageConnection.Close();
}