检索错误(参数无效)但我的列名是image

时间:2014-05-15 22:46:36

标签: c#

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();
}

1 个答案:

答案 0 :(得分:0)

'image'作为列名无效。你需要'[image]':

select empid,empname,salary,gender,[image] from emp where empid = {0}

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