存在用於從數據庫加載圖像並將其顯示在圖片框中的代碼。問題是如果沒有連續的圖片,它會遇到一個錯誤,但我標記爲允許數據庫中的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