这是我遇到的问题。我有一个包含几列的 SQL Server 表。其中之一是image
数据类型。图像保存为二进制数据。
在一个表单上,我有一个 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();
}
}
}
我认为这是一个语法错误,但我不知道在哪里或如何修复它。