2017-04-03 202 views
0

enter image description here這裏我查詢,如果任何人能發現錯誤UPDATE查詢語法錯誤的MySQL

str = "update student set course='" & ComboBox1.Text & "',name='" & 
TextBox2.Text & "',f_name='" & TextBox3.Text & "',address='" & TextBox4.Text 
& "' ,tel_no='" & TextBox5.Text & "',qualification='" & TextBox6.Text & 
"',remarks='" & TextBox7.Text & "',school/college='" & TextBox8.Text & 
"',fee='" & TextBox10.Text & "' where reg_no=" & TextBox9.Text & " " 
+0

我在哪裏添加一個from子句?我從來沒有使用從條款更新 – sunshine

+0

其中是錯誤消息 – Cherif

+0

並且這是一個列的名稱?學校/學院 – Cherif

回答

1

下面是構建這個查詢一個更好的辦法:

str = "update student " & 
     " set course= @course, name= @name, f_name= @fname, address= @address," & 
     " tel_no= @tel, qualification = @qualification, remarks= @remarks," & 
     " `school/college`[email protected], fee= @fee" & 
     " where reg_no= @regno" 

Using cn As New MySqlConnection("connection string here"), _ 
     cmd As New MySqlCommand(str, cn) 

    'Use actual column types/lengths from your DB here 
    cmd.Parameters.Add("@course", MySqlDbType.VarChar, 15).Value = ComboBox1.Text 
    cmd.Parameters.Add("@name", MySqlDbType.VarChar, 25).Value = TextBox2.Text 
    cmd.Parameters.Add("@fname", MySqlDbtype.VarChar, 25).Value = TextBox3.Text 
    cmd.Parameters.Add("@address", MySqlDbType.VarChar, 120).Value = TextBox4.Text 
    cmd.Parameters.Add("@tel", MySqlDbType.VarChar, 25).Value = TextBox5.Text 
    cmd.Parameters.Add("@qualification", MySqlDbType.VarChar, 40).Value = TextBox6.Text 
    cmd.Parameters.Add("@remarks", MySqlDbType.VarString).Value = TextBox7.Text 
    cmd.Parameters.Add("@school", MySqlDbType.VarChar, 40).Value = TextBox8.Text 
    cmd.Parameters.Add("@fee", MySqlDbType.Decimal, 6, 2).Value = Convert.ToDecimal(TextBox10.Text) 
    cmd.Parameters.Add("@regno", MySqlDbType.Int32).Value = Integer.Parse(TextBox9.Text) 

    cn.Open() 
    cmd.ExecuteNonQuery() 
End Using 

這確實一批的事情你:

  1. 它可以防止SQL注入攻擊
  2. 它允許您接受包含單引號之類的數據(')。如果有人輸入單引號,你的代碼將會失敗。
  3. 它自動處理sql日期格式。
  4. 速度更快,因爲數據庫服務器可以在編譯查詢後緩存執行計劃,並且可以使用統計數據來獲得更好的執行計劃。
  5. 它更可靠地關閉數據庫連接。如果拋出異常,當前的代碼將使數據庫連接懸而未決。
+0

謝謝你的幫助:) – sunshine