这是我遇到的问题。我有一个包含几列的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