2013-10-20 27 views
0

我的update()方法遇到了一些問題。這個想法是,用戶提供配方名稱,成分,說明,然後使用文件流選擇一個圖像。將數據插入表中的語法不正確

一旦用戶點擊「添加配方」,這將調用Update方法,但就當前情況來看,我越來越被提的文本框的內容的錯誤:

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

任何人都可以看到我已經錯的INSERT INTO食譜查詢或建議的替代方法的代碼的一部分?

+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 +」')「;

相關問題