這是我遇到的問題。我有一個包含幾列的SQL Server表。其中之一是image
數據類型。圖像保存爲二進制數據。如何在c中的datagridview中選擇記錄時顯示存儲在數據庫表中的圖像#
在窗體上,我有一個dataGridView,顯示錶中的3列。他們都沒有包含圖像。當我點擊dataGridView中的一條記錄時,我希望圖像顯示在同一個表單中。
這是我到目前爲止的代碼:
public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection cn = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True");
if (e.RowIndex >= 0) {
DataGridViewRow row = dataGridView1.SelectedRows[0];
textBox1.Text = row.Cells["anf"].Value.ToString();
textBox2.Text = row.Cells["putere"].Value.ToString();
textBox3.Text = row.Cells["caprez"].Value.ToString();
textBox4.Text = row.Cells["greutate"].Value.ToString();
textBox5.Text = row.Cells["stoc"].Value.ToString();
textBox6.Text = row.Cells["pret"].Value.ToString();
textBox7.Text = row.Cells["garantie"].Value.ToString();
SqlCommand cmd = new SqlCommand("select poza from motociclete where codm '" + dataGridView1.SelectedRows + "'", cn);
cn.Open();
try
{
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] picarr = (byte[])dr["poza"];
MemoryStream ms = new MemoryStream(picarr);
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = Image.FromStream(ms);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally {
cn.Close();
}
}
}
我認爲這是一個語法錯誤,但我不知道在哪裏或如何解決它。
我做了,但它仍然給我同樣的錯誤:附近'不正確的語法附近'system.windows.forms.datagridviewselectedcellcollection' – stefan9976
'但我不我認爲這個錯誤與這個圖片問題有關。看起來你可能在別的地方有錯誤。 – Sachin
你有什麼想法在哪裏?文本框正在工作,所以我認爲它從sql表中讀取的很好。 – stefan9976