2013-12-16 121 views
-3

我設法編寫此代碼以便從SQL Server數據庫中刪除記錄。無法刪除數據庫中的記錄(SQL服務器)

Private sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click 

    Dim strconnection As String = "Data Source=EASMAR-PC;Initial Catalog=DatabaseConnection;Integrated Security=True;" 

    Dim _cn As SqlConnection = New SqlConnection(strconnection) 
    Dim cmd As New SqlCommand 
    _cn.Open() 
    Try 
     _DataSet.Tables(0).Rows(CInt(txtCurrent.Text) - 1).Delete() 

     Dim adapter As New SqlDataAdapter 
     Dim cmdBuilder As New SqlCommandBuilder(adapter) 
     Dim DutyDetails As String = "SELECT ID from tblCustomer" 

     adapter.SelectCommand = New SqlCommand(DutyDetails, _cn) 
     adapter.UpdateCommand = cmdBuilder.GetUpdateCommand 
     adapter.DeleteCommand = cmdBuilder.GetDeleteCommand 

     Dim cb As SqlCommandBuilder = New SqlCommandBuilder(adapter) 

     adapter.Update(_DataSet.Tables("0")) 
End sub 

我錯過了什麼嗎?

我沒有收到任何錯誤。但記錄仍然存在。

任何幫助?

比你:)

+0

我不這麼認爲。我檢查了:/ – user2968155

+2

我看不到你實際上調用了deleteCommand。 –

回答

0

試試這個:

Private Sub DeleteRecord(recordID As Integer) 
    ' define connection string 
    Dim connString As String = "Data Source=EASMAR-PC;Initial Catalog=DatabaseConnection;Integrated Security=True;" 

    ' define SQL statement to execute, using a **PARAMETRIZED** query 
    Dim deleteStmt As String = "DELETE FROM dbo.tblCustomer WHERE CustomerID = @ID" 

    Using conn As New SqlConnection(connString) 
     Using cmd As New SqlCommand(deleteStmt, conn) 
      ' define parameter 
      cmd.Parameters.Add("@ID", SqlDbType.Int).Value = recordID 

      ' open connection, execute DELETE, close connection 
      conn.Open() 
      cmd.ExecuteNonQuery() 
      conn.Close() 
     End Using 
    End Using 
End Sub