1

我的这个功能工作了一半。工作正常的部分是我可以在 DataGridView 上选择一行,使用“删除行”按钮调用此函数,然后它将从 DataGridView 中删除该行....但是,它不会删除该行数据库上。

谁能帮助我使用 OleDb 从数据库中删除该行?

Function DeleteTableRow()
    Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
    Dim dbConnection = New OleDbConnection(TaxConnStr)

    Try
        Dim dbCommand As OleDbCommand = New OleDbCommand
        Dim rdr2 As OleDbDataReader

        Dim selectedRow = DataGridView1.SelectedRows

        dbCommand.CommandText = "DELETE FROM UserCriteria WHERE RowID =" & selectedRow
        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If

        dbCommand.Connection = dbConnection
        rdr2 = dbCommand.ExecuteReader
        dbCommand.ExecuteNonQuery()


        rdr2.Close()

        '''Must select entire row to delete
        'DataGridView1.Rows.Remove(DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex))

        '''allows you to select on cell in the row to delete entire row
        For Each oneCell As DataGridViewCell In DataGridView1.SelectedCells
            If oneCell.Selected Then
                DataGridView1.Rows.RemoveAt(oneCell.RowIndex)
            End If
        Next



    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        dbConnection.Close()
    End Try
End Function
4

3 回答 3

2

DataGridView.SelectedRows是一个集合DataGridViewRow,不能使用集合作为参数来删除数据库表上的特定记录。(您是否将OPTION STRICT 设置为关闭?)

您需要遍历集合,ID从每一行获取正确的值并将该值用作删除查询的参数。

If dbConnection.State = ConnectionState.Closed Then
    dbConnection.Open()
End If

' Creating the command and its parameter here before entering the loop to avoid a continue'
' create and destroy pattern for the OleDbCommand'
Dim dbCommand As OleDbCommand = New OleDbCommand
dbCommand.CommandText = "DELETE FROM UserCriteria WHERE ID =?"
dbCommand.Connection = dbConnection
dbCommand.Parameters.AddWithValue("@row", 0) 
Dim rows = DataGridView1.SelectedRows
For Each row in rows
    dbCommand.Parameters("@row").Value = row.Cells("ID").Value)
    dbCommand.Connection = dbConnection
    dbCommand.ExecuteNonQuery()
Next

还要注意不要使用字符串连接来构建 sql 命令。这种习惯导致了一种叫做 Sql Injection 的蠕虫

当然,这里不需要 OleDbDataReader。(没有什么可读的)

于 2013-03-27T16:19:57.510 回答
1

您不需要阅读器来删除一行。不会返回任何数据

   rdr2 = dbCommand.ExecuteReader
    dbCommand.ExecuteNonQuery()


    rdr2.Close()

应该只是

    dbCommand.ExecuteNonQuery()
于 2013-03-27T16:16:42.010 回答
0

问题是您DataGridView1.SelectedRows将返回一个SelectedRowCollection(对不起,我假设这是一个 WinForms 应用程序)。当传递给您时,这不会为您提供正确的结果,CommandText因为您可能会获得 ToString() 的SelectedRowCollection而不是您所追求的 ID

您真正想要做的是遍历集合(如果用户能够选择多行)并删除选择的每一行,例如:

For Each selectedRow in DataGridView1.SelectedRows
    '1. Get the DatabaseId of the selected row
    '2. Modify dbCommand.CommandText to use the selected row from 1
    '3. execute command like you are doing with ExecuteNonQuery
Next

上面的每个selectedRow都属于这种类型......它有一个Cells属性,您可以访问以获取您需要的 ID(我不确定它将在哪个单元格中,但您应该能够从您的代码中分辨出来)。

于 2013-03-27T16:21:12.490 回答