存在用于从数据库加载图像并将其显示在图片框中的代码。问题是如果没有连续的图片,它会遇到一个错误,但我标记为允许数据库中的nulls图像列。从数据库加载图像列图像,其中图像列可能为空
private void button1_Click(object sender, EventArgs e)
{
sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
cmd = new SqlCommand();
cmd.Connection = sql;
cmd.CommandText = ("select Image from Entry where EntryID [email protected]");
cmd.Parameters.AddWithValue("@EntryID", Convert.ToInt32(textBox1.Text));
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Images");
int count = ds.Tables["Images"].Rows.Count;
if (count > 0)
{
var data = (Byte[])(ds.Tables["Images"].Rows[count - 1]["Image"]);
var stream = new MemoryStream(data);
pictureBox1.Image= Image.FromStream(sream);
}
}
什么是错误? – zimdanen
sream是一个错字还是你有两个不同的流('sream'&'var stream ...')? –
无法投射'system.dbnull'类型的对象来键入'system.byte []' – aliprogrammer