使用OleDb从数据库中删除行

时间:2013-03-27 15:50:36

标签: vb.net row oledb

我有这个功能正常工作。正常工作的部分是我可以在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

3 个答案:

答案 0 :(得分:2)

DataGridView.SelectedRowsDataGridViewRow的集合,您不能使用集合作为参数来删除数据库表上的特定和特定记录。 (你有OPTION STRICT set tot OFF吗?)

您需要遍历集合,从每一行获取正确的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。 (没什么可读的)

答案 1 :(得分:1)

您不需要读者删除行。不会返回任何数据

   rdr2 = dbCommand.ExecuteReader
    dbCommand.ExecuteNonQuery()


    rdr2.Close()

应该只是

    dbCommand.ExecuteNonQuery()

答案 2 :(得分:0)

问题是您的DataGridView1.SelectedRows会返回SelectedRowCollection(抱歉,我假设这是一个WinForms应用)。当传递给CommandText时,这不会得到正确的结果,因为您可能会得到SelectedRowCollection的ToString()而不是您之后的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都是this类型的...有一个Cells属性,您可以访问该属性以获取所需的ID(我不确定它是哪个单元格将进入,但你应该能够从你的代码中说出来。)