2014-05-15 4 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"가 열 이름으로 잘못되었습니다. '[image]'를 시도해보십시오. –

+0

DB 엔진이 허용하더라도 키워드를 개체 이름으로 사용하는 것은 일반적으로 바람직하지 않습니다. '[] '로 묶으면 작업하기가 어렵습니다. – Alejandro

답변

0

'image'는 열 이름으로 잘못되었습니다. [image]가 필요합니다 :

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

또한 "SQL Injection"을 검색하여 코드가 위험한 이유를 알아보십시오.

관련 문제