2015-01-07 90 views
1

我试图用下面的代码在按钮上运行我的系统,但是我得到一个很长的错误(由于我没有足够的声望,我会发布一个链接到截图。)谢谢! http://i.imgur.com/MM2GP00.png 这里是代码:长更新语句错误?

Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click 
     Try 
      Dim sqlquery As String = "UPDATE books SET Title = @titl, Author = @aut, " & _ 
         "WHERE ID = " & DataGridView2.SelectedRows(0).Cells(0).Value.ToString & ";" 

      ' Use this form to initialize both connection and command to 
      ' avoid forgetting to set the appropriate properties.... 

      Using conn = New System.Data.OleDb.OleDbConnection(cnString) 
       Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, conn) 

        conn.Open() 
        cmd.Parameters.AddWithValue("@titl", TextBox2.Text) 
        cmd.Parameters.AddWithValue("@aut", TextBox3.Text) 

        If TextBox2.Text = "" Or TextBox3.Text = "" Then 
         MessageBox.Show("Please complete the required fields.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
         Return 
        Else 
         Dim rowsInserted = cmd.ExecuteNonQuery() 
         If rowsInserted > 0 Then 
          MessageBox.Show("A record has been successfully updated!", "Updated!") 
          dtgrd() 
         Else 
          MessageBox.Show("Failed to update record!", "Failure!") 
         End If 
        End If 

       End Using 
      End Using 
     Catch ex As Exception 
      MsgBox(ex.ToString) 
     End Try 
    End Sub 
+0

'Author = @ aut'后面有一个逗号,将其删除。这只是一个简单的错字,你可以修复它,并删除这个问题,如果你不想被downvotes淹没。 (作为一个附注,尝试阅读有关参数化查询以及为什么您应该始终将它们用于传递给数据库的每个值) – Steve

回答

0

你有Author = @aut,你的SQL查询额外的逗号,尝试删除

替换此代码中的

Dim sqlquery As String = "UPDATE books SET Title = @titl, Author = @aut " & _ 
    "WHERE ID = " & DataGridView2.SelectedRows(0).Cells(0).Value.ToString & ";" 

还有一件事,你在打开连接之后在代码中检查文本框的空值,尝试在做任何事之前进行验证。然后你的代码将会是这样的

Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click 
    Try   
    If TextBox2.Text = "" Or TextBox3.Text = "" Then 
     MessageBox.Show("Please complete the required fields.", "Admin", MessageBoxButtons.OK, 
      MessageBoxIcon.Exclamation) 
     Return 
    End If 

    Dim sqlquery As String = "UPDATE books SET Title = @titl, Author = @aut " & _ 
      "WHERE ID = " & DataGridView2.SelectedRows(0).Cells(0).Value.ToString & ";" 

    ' Use this form to initialize both connection and command to 
    ' avoid forgetting to set the appropriate properties.... 

    Using conn = New System.Data.OleDb.OleDbConnection(cnString) 
    Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, conn) 

    conn.Open() 
    cmd.Parameters.AddWithValue("@titl", TextBox2.Text) 
    cmd.Parameters.AddWithValue("@aut", TextBox3.Text) 

    Dim rowsInserted = cmd.ExecuteNonQuery() 
    If rowsInserted > 0 Then 
     MessageBox.Show("A record has been successfully updated!", "Updated!") 
     dtgrd() 
    Else 
     MessageBox.Show("Failed to update record!", "Failure!") 
    End If 
    End Using 
    End Using 
    Catch ex As Exception 
     MsgBox(ex.ToString) 
    End Try 
End Sub