简体   繁体   English

VB.NET在datagridview的Access数据库中保存delete命令

[英]VB.NET Save the delete command in access database in datagridview

I have working code that uses a SQL command to delete a select row from a database and that command works if I use it in access. 我有使用SQL命令从数据库中删除选择行的工作代码,并且如果我在访问中使用它,该命令也会起作用。 However, when I want to do this with my delete button in VB.NET it does not save it. 但是,当我要使用VB.NET中的delete按钮执行此操作时,它不会保存它。 When I check in program if it is gone it works and updates everything and so forth. 当我检查程序是否消失时,它可以工作并更新所有内容,依此类推。 When I exit the program and start it again it goes back to normal. 当我退出程序并再次启动它时,它又恢复了正常。 I need help with the fix badly ! 我急需修复方面的帮助!

Imports System.Data.OleDb

Public Class CustomersForm
    Dim Contact(-1) As CustomerData
    ''' <summary>
    ''' Load Form and Fill Table Adapter
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub CustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MooselyAdventuresDataSet.tblCustomers' table. You can move, or remove it, as needed.
        Me.TblCustomersTableAdapter.Fill(Me.MooselyAdventuresDataSet.tblCustomers)
        LoadCustomerInformation()
    End Sub
    ''' <summary>
    ''' Structure for data for datagrid view and to hold data for cells
    ''' </summary>
    ''' <remarks></remarks>
    Structure CustomerData
        Public LongCustomerID As Long
        Public FirstName As String
        Public LastName As String
        Public Address As String
        Public City As String
        Public State As String
        Public Zip As Integer
        Public Email As String
    End Structure
    ''' <summary>
    ''' Close Customers Form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btCustClose_Click(sender As Object, e As EventArgs) Handles btCustClose.Click
        Me.Close()
    End Sub
    ''' <summary>
    ''' Call the Sql statement and retrieve data and fill the datagridview control
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadCustomerInformation()
        dgvCustomers.Rows.Clear()
        dgvCustomers.Columns.Clear()
        Dim CustData As New CustomerInfoGetter

        'Set customer string to the module
        CustData.ConnectionString = mdlConnectString.ConnectionString
        CustData.Sql = "SELECT CustomerLast, CustomerFirst, CustomerAddress, CustomerCity, CustomerState, CustomerZip, CustomerEmail, CustomerID FROM tblCustomers"
        'Add Columns for datagrid view
        dgvCustomers.Columns.Add("First Name", "First Name")
        dgvCustomers.Columns.Add("Last Name", "Last Name")
        dgvCustomers.Columns.Add("Street Address", "Street Address")
        dgvCustomers.Columns.Add("City", "City")
        dgvCustomers.Columns.Add("State", "State")
        dgvCustomers.Columns.Add("Zip", "Zip")
        dgvCustomers.Columns.Add("Email", "Email")
        dgvCustomers.Columns.Add("CustomerID", "CustomerID")



        For I As Integer = 0 To CustData.DS.Tables(0).Rows.Count - 1
            ReDim Preserve Contact(Contact.Length)
            With Contact(Contact.Length - 1) 'loop to add data to datagridview and getting data from sql statement'
                .FirstName = CustData.DS.Tables(0).Rows(I).Item("CustomerFirst").ToString()
                .LastName = CustData.DS.Tables(0).Rows(I).Item("CustomerLast").ToString()
                .Address = CustData.DS.Tables(0).Rows(I).Item("CustomerAddress").ToString()
                .City = CustData.DS.Tables(0).Rows(I).Item("CustomerCity").ToString()
                .State = CustData.DS.Tables(0).Rows(I).Item("CustomerState").ToString()
                .Zip = CustData.DS.Tables(0).Rows(I).Item("CustomerZip").ToString()
                .Email = CustData.DS.Tables(0).Rows(I).Item("CustomerEmail").ToString()
                .LongCustomerID = CustData.DS.Tables(0).Rows(I).Item("CustomerID").ToString()
                dgvCustomers.Rows.Add() 'add a row to fill information'
                dgvCustomers.Rows(I).Cells(0).Value = .FirstName
                dgvCustomers.Rows(I).Cells(1).Value = .LastName
                dgvCustomers.Rows(I).Cells(2).Value = .Address
                dgvCustomers.Rows(I).Cells(3).Value = .City
                dgvCustomers.Rows(I).Cells(4).Value = .State
                dgvCustomers.Rows(I).Cells(5).Value = .Zip
                dgvCustomers.Rows(I).Cells(6).Value = .Email
                dgvCustomers.Rows(I).Cells(7).Value = .LongCustomerID


            End With
        Next

        dgvCustomers.Columns(7).Visible = False 'Make Last Row Invisible'
    End Sub
    ''' <summary>
    ''' When a cell is clicked (Actually entire rows are only selected....) it will changed the textboxes and combo box to according
    ''' values
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgvCustomers_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCustomers.CellClick
        Dim i As Integer = dgvCustomers.CurrentRow.Index

        'Fill Text Boxes and Such When Clicked'
        tbLastName.Text = dgvCustomers.Rows(i).Cells(1).Value
        tbFirstName.Text = dgvCustomers.Rows(i).Cells(0).Value
        tbStreet.Text = dgvCustomers.Rows(i).Cells(2).Value
        tbCity.Text = dgvCustomers.Rows(i).Cells(3).Value
        cbStates.Text = dgvCustomers.Rows(i).Cells(4).Value
        tbZipCode.Text = dgvCustomers.Rows(i).Cells(5).Value
        tbEmail.Text = dgvCustomers.Rows(i).Cells(6).Value

    End Sub

    Private Sub btDelete_Click(sender As Object, e As EventArgs) Handles btDelete.Click

        Try
            Dim CustData As New CustomerInfoGetter
            Dim CustIDDelete As String


            CustIDDelete = dgvCustomers.Rows(dgvCustomers.CurrentRow.Index).Cells(7).Value

            'Set customer string to the module
            CustData.ConnectionString = mdlConnectString.ConnectionString
            CustData.Sql = "DELETE FROM tblCustomers WHERE CustomerID = " & CustIDDelete
            LoadCustomerInformation()


        Catch
            MessageBox.Show("You Can't Delete When Their Are No Records !", "Error !")
        End Try

    End Sub

    Private Sub btAdd_Click(sender As Object, e As EventArgs) Handles btAdd.Click

    End Sub
End Class

I am not sure what you're doing exactly in your code but I have written a little function to do what you want 我不确定您在代码中到底在做什么,但是我写了一个小函数来做您想要的

Public Shared Function CreateCustomerAdapter( _
    connection As OleDbConnection) As OleDbDataAdapter 

    Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim command As OleDbCommand
    Dim parameter As OleDbParameter

    ' Your DELETE command.
    command = New OleDbCommand( _
        "DELETE * FROM tblCustomers WHERE CustomerID = ?", _
        connection)

    parameter = command.Parameters.Add( _
        "CustomerID", OleDbType.Char, 5, "CustomerIDDelete")

    parameter.SourceVersion = DataRowVersion.Original

    dataAdapter.DeleteCommand = command

    Return dataAdapter
End Function

and then 接着

command.ExecuteNonQuery()

Last but not least, remember to use Parametrization on your SQL sentence since it will follow the good practice and prevent security issues such as SQL injection. 最后但并非最不重要的一点,请记住在SQL语句上使用参数化,因为它会遵循良好的做法并防止出现SQL注入之类的安全性问题。 That's what OleDbParameter is for. 那就是OleDbParameter目的。 Check the MSDN page below for more info 检查下面的MSDN页面以获取更多信息

OleDbCommand.Parameters Property OleDbCommand.Parameters属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM