2014-05-15 24 views
0
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(); 
} 
+0

“图像”作为列名无效。试试'[image]'。 –

+0

尽管数据库引擎允许使用关键字作为对象名称,但这通常是不好的做法。将它们放在'[]'中的要求使它们很难处理。 – Alejandro

回答

0

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

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

此外,我建议你搜索“SQL注入”以了解为什么你的代码是危险的。