从数据网格视图中删除记录

时间:2016-12-28 07:52:26

标签: mysql sql visual-studio visual-studio-2010 datagridview

请帮帮我。我在我正在创建的支票打印系统中添加了一个删除方法,在它运行良好之前,但是当我将字段Check_Number的数据类型从text更改为number时它们各自的表,它不再删除我选择的数据行。我不记得从删除部分更改任何代码。你们有任何想法如何解决这个问题吗?先感谢您。更多力量!

不同的Bank_ [BankName]具有ff字段和数据类型:

ID - AutoNumber 
Check_Number - Number
Company_ID - Text

我的VN_ZAM表包含ff字段和数据类型:

ID - AutoNumber
Voucher_Number - Number
Check_Number - Text
Bank_Type - Text
Company_ID - Text   

我的SS表有ff字段和数据类型(这也是数据网格视图中数据的来源):

ID - AutoNumber
Check_No - Text
Voucher_No - Text
Issue_Date - Date/Time
Company_Name - Text
Bank_Type - Text 
Amount_in_Figure - Number
Amount_in_Words - Text
Payee - Text

我的datagridview顺序如下所示:

ID | Check_No | Voucher_No | Issue_Date | Company_Name |Bank_Type | Amount_in_Figure | Amount_in_Words | Payee

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
    Dim sql As String
    Dim sql1 As String
    Dim sql2 As String
    Dim answer As Integer

    answer = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbDefaultButton1, "Information")
    'If answer = vbYes Then
    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    dataFile = "C:\Users\Pc\Documents\Visual Studio 2010\Projects\bankCheckSystem\dbcheckprintsystem.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString
    myConnection.Open()
    sql = "DELETE * FROM SS WHERE ID =" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString()

    If dgvSSCheckRecords.SelectedRows(0).Cells(12).Value.ToString() = "ZAM" Then
        sql1 = "DELETE * FROM VN_ZAM WHERE Company_ID = '" & dgvSSCheckRecords.SelectedRows(0).Cells(4).Value.ToString() & "_" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString() & "' "
        Dim cmd As OleDbCommand = New OleDbCommand(sql, myConnection)
        Dim cmd1 As OleDbCommand = New OleDbCommand(sql1, myConnection)

        'cmd.ExecuteNonQuery()
        'cmd1.ExecuteNonQuery()

        If dgvSSCheckRecords.SelectedRows(0).Cells(5).Value.ToString() = "BDO" Then
            sql2 = "DELETE * FROM Bank_BDO WHERE Company_ID = '" & dgvSSCheckRecords.SelectedRows(0).Cells(4).Value.ToString() & "_" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString() & "' AND Check_Number ='" & dgvSSCheckRecords.SelectedRows(0).Cells(1).Value.ToString() & "'"
            Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, myConnection)

            cmd.ExecuteNonQuery()
            cmd1.ExecuteNonQuery()
            cmd2.ExecuteNonQuery()
        ElseIf dgvSSCheckRecords.SelectedRows(0).Cells(5).Value.ToString() = "BPI" Then
            sql2 = "DELETE * FROM Bank_BPI WHERE Company_ID = '" & dgvSSCheckRecords.SelectedRows(0).Cells(4).Value.ToString() & "_" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString() & "' AND Check_Number ='" & dgvSSCheckRecords.SelectedRows(0).Cells(1).Value.ToString() & "'"
            Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, myConnection)
            cmd.ExecuteNonQuery()
            cmd1.ExecuteNonQuery()
            cmd2.ExecuteNonQuery()
        End if

End Sub

1 个答案:

答案 0 :(得分:0)

正如草莓所暗示的,我找不到DELETE * FROM有效语法的任何例子(我不确定你可以像SELECT * FROM一样使用它吗?)。此外,如果银行表格中的Check_Number已更改为number,我不确定您是否需要围绕值的单引号。如果将四个SQL语句更改为以下内容会有帮助吗?

  1. sql = "DELETE FROM SS WHERE ID = " & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString()
  2. sql1 = "DELETE FROM VN_ZAM WHERE Company_ID = '" & dgvSSCheckRecords.SelectedRows(0).Cells(4).Value.ToString() & "_" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString() & "' "
  3. sql2 = "DELETE FROM Bank_BDO WHERE Company_ID = '" & dgvSSCheckRecords.SelectedRows(0).Cells(4).Value.ToString() & "_" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString() & "' AND Check_Number = " & dgvSSCheckRecords.SelectedRows(0).Cells(1).Value.ToString()
  4. sql2 = "DELETE FROM Bank_BPI WHERE Company_ID = '" & dgvSSCheckRecords.SelectedRows(0).Cells(4).Value.ToString() & "_" & dgvSSCheckRecords.SelectedRows(0).Cells(0).Value.ToString() & "' AND Check_Number = " & dgvSSCheckRecords.SelectedRows(0).Cells(1).Value.ToString()