C# 检索错误(参数无效),但我的列名为image

C# 检索错误(参数无效),但我的列名为image,c#,C#,“image”作为列名无效。您需要“[图像]”: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection con = new SqlConnection(Helper.ConnectionString); SqlCommand cmd = new SqlCommand(); string sql = string.Format("select empid,em

“image”作为列名无效。您需要“[图像]”:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   SqlConnection con = new SqlConnection(Helper.ConnectionString);
   SqlCommand cmd = new SqlCommand();
   string sql = string.Format("select empid,empname,salary,gender,image from emp where empid = {0}",comboBox1.Text);
   cmd.CommandText = sql;
   cmd.Connection = con;

   con.Open();

   SqlDataReader dr = cmd.ExecuteReader();
   int indid = dr.GetOrdinal("empid");
   int indname = dr.GetOrdinal("empname");
   int indsalary = dr.GetOrdinal("salary");
   int indgender = dr.GetOrdinal("gender");

   while (dr.Read())
   {
      int id = dr.GetInt32(indid);
      textBox1.Text = id.ToString();
      textBox2.Text = dr.GetString(indname);
      textBox3.Text = dr.GetDecimal(indsalary).ToString();
      string gen = dr.GetString(indgender);
      if (gen == "Male")
         radioButton1.Checked = true;
      else
         radioButton2.Checked = true;

      byte[] imgg = (byte[])(dr["image"]);
      if (imgg == null)
         pictureBox1.Image = null;
      else
      {
         using

            (MemoryStream mstream = new MemoryStream(imgg))

            pictureBox1.Image = System.Drawing.Image.FromStream(mstream);

      }
   }
   con.Close();
}

此外,我建议您搜索“SQL注入”以了解代码的危险性。

“图像”作为列名无效。尝试
[image]
。使用关键字作为对象名通常是不好的做法,即使DB引擎允许这样做。将它们封装在
[]
中的要求使它们难以使用。
select empid,empname,salary,gender,[image] from emp where empid = {0}