我正在 c# 桌面应用程序中创建项目。我想添加一些功能,比如当我从数据网格视图中选择行时,数据库中的图像会显示在图片框中。但有一些错误,如:
“参数无效”
我的代码是..
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
// display content
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
label2.Text = value1;
label4.Text = value2;
//Display Image
SqlConnection cn = new SqlConnection();
string str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\PROJECT\\NEW\\CASTING CALCULATING SYSTEM\\CASTING CALCULATING SYSTEM\\DB_CASTING.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True;";
cn.ConnectionString = str;
SqlCommand cmd = new SqlCommand ();
cmd.Connection = cn;
string strsql = "select image from EmpMaster WHERE Fname = '" +value2+ "'";
cmd.CommandText = strsql ;
cn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] picarr = (byte[])dr["image"];
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();
}
}
}