2013-10-20 2 views
0

update() 메서드에 문제가 있습니다. 아이디어는 사용자가 조리법 이름, 재료, 지침을 제공 한 다음 Filestream을 사용하여 이미지를 선택한다는 것입니다.테이블에 데이터를 삽입하는 구문이 잘못되었습니다.

다음

enter image description here

가 있습니다 : 사용자가 클릭이 일이 내가 텍스트 상자의 내용을 언급하는 오류를 얻고 서 그러나로, update 메소드를 호출합니다 '레시피 추가'를 한 번

갱신() 메소드 코드 : 나는 조리법 쿼리에 삽입하여 잘못 됐을 또는 코드의이 부분에 대한 대안 접근 방식을 제시 한 곳

private void updatedata() 

     { 
     // filesteam object to read the image 
     // full length of image to a byte array 

      try 
      { 
       // try to see if the image has a valid path 

       if (imagename != "") 
       { 

        FileStream fs; 
        fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read); 

        // a byte array to read the image 

        byte[] picbyte = new byte[fs.Length]; 
        fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length)); 
        fs.Close(); 

        //open the database using odp.net and insert the lines 

        string connstr = @"Server=mypcname\SQLEXPRESS;Database=RecipeOrganiser;Trusted_Connection=True"; 

        SqlConnection conn = new SqlConnection(connstr); 
        conn.Open(); 
        string query; 
        query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) values (" + textBox1.Text + "," + " @pic" + "," + textBox2.Text + "," + textBox3.Text + ")"; 
        SqlParameter picparameter = new SqlParameter(); 
        picparameter.SqlDbType = SqlDbType.Image; 
        picparameter.ParameterName = "pic"; 
        picparameter.Value = picbyte; 
        SqlCommand cmd = new SqlCommand(query, conn); 
        cmd.Parameters.Add(picparameter); 
        cmd.ExecuteNonQuery(); 
        MessageBox.Show("Image successfully saved"); 
        cmd.Dispose(); 
        conn.Close(); 
        conn.Dispose(); 
        Connection(); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

사람이 볼 수 있을까요?

+3

는 표준화 된 디자인이 아닌 – ffffff01

+1

이 또한 하나의 컬럼에 모든 RecipeIngredients 우는 .. 매개 변수화 쿼리를 사용하십시오 당신은'using' 블록을 사용한다. –

+0

텍스트 값을 인용하지 않습니다. 각 값의 앞뒤에 작은 따옴표 (')를 넣으십시오. –

답변

3

코드는 SQL 주입 열려 있지만, 아마 당신의 오류는 작은 따옴표 (예를 들어 설명 필드)를 포함 텍스트에서오고이 사용자 입력을 연결 사용하여 명령 문자열 빌드를 휴식.

편집 사람이 코멘트에 지적 오류가 당신의 텍스트 상자 주위에 누락 된 따옴표에 의해 발생합니다. 그러나 쉽게 해결할 수있는 방법은 누락 된 따옴표를 추가하는 오류를 수정하는 것이 잘못 되었기 때문에 갈 길이 아닙니다. 그것은 악용되기를 기다리고있는 커다란 보안 구멍을 남겨두고 문제를 연기하는 것입니다.

매개 변수가있는 쿼리는이 모든 혼란을 피할 수 있습니다. 당신이 문자열 회씩 연결을 사용하기 때문에

string connstr = "....";  
    string query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) " + 
      "values (@name, @pic, @ing, @instr)"; 
    using(SqlConnection conn = new SqlConnection(connstr)) 
    using(SqlCommand cmd = new SqlCommand(query, conn)) 
    { 
    conn.Open(); 
    SqlParameter picparameter = new SqlParameter(); 
    picparameter.SqlDbType = SqlDbType.Image; 
    picparameter.ParameterName = "@pic"; 
    picparameter.Value = picbyte; 
    cmd.Parameters.Add(picparameter); 
    cmd.Parameters.AddWithValue("@name", textbox1.Text); 
    cmd.Parameters.AddWithValue("@ing", textbox2.Text); 
    cmd.Parameters.AddWithValue("@instr", textbox3.Text); 
    cmd.ExecuteNonQuery(); 
    MessageBox.Show("Image successfully saved"); 
    } 
+2

이 경우에는 작은 따옴표를 사용하지 않기 때문에 발생합니다. "연어 파스타"라는 구에는 작은 따옴표가 없습니다. 그들은 여전히 ​​다른 세 가지 값을 매개 변수화해야합니다. –

+0

두 개의 문자열 쿼리가 있어야합니다. 선언은 부모 범위에서 이미 선언 되었기 때문에? – JsonStatham

+0

나의 실수, 아니, 단지 하나. 이제 남은, 청소 .... – Steve

3

, 당신은 아마 견적을 놓친하거나 별도의 견적을 넣어 또는 쉼표를 놓친거나 .... 쉼표 등 등 추가

이 방법을 사용하지 마십시오 넣어!

귀하의 오류가 분명히 보이지 않지만 항상 parameterized queries을 사용해야합니다. 이러한 종류의 문자열 연결은 SQL Injection 공격에 대해 열려 있습니다.

query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) values (@p1, @pic, @p3, @p4)"; 
SqlCommand cmd = new SqlCommand(query, conn); 
cmd.Parameters.AddWithValue(@p1, textBox1.Text); 
cmd.Parameters.AddWithValue(@pic, textBox1.Text); 
cmd.Parameters.AddWithValue(@p3, textBox1.Text); 
cmd.Parameters.AddWithValue(@p4, picparameter); 
1

"(조리법 (RecipeName, RecipeImage, RecipeIngredients, RecipeInstructions) 값으로 삽입 '"=이

쿼리를 시도 + textBox1.Text + "'"+ "@pic"+ ", ' "+ textBox2.Text +" ',' "+ textBox3.Text +" ') ";

관련 문제