2015-02-09 54 views
1

我有工作代碼,它使用SQL命令從數據庫中刪除選擇行,如果我在訪問中使用它,該命令將起作用。但是,當我想在VB.NET中使用我的delete按鈕進行此操作時,它不會保存它。當我在程序中檢查它是否消失時,它會工作並更新所有內容等等。當我退出程序並重新啓動時,它會恢復正常。我需要幫助解決這個問題!VB.NET將刪除命令保存在訪問數據庫中的datagridview

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 
+0

您正在形成一個SQL字符串,但你不通過Command對象執行它的詳細信息。在刪除情況下永遠不會調用數據庫。請參閱此例如:https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.deletecommand%28v=vs.110%29.aspx – NoChance 2015-02-09 01:15:05

+0

是您的'CustomerInfoGetter.Sql'設置程序,無論如何,調用SQL? – Ripple 2015-02-09 01:30:49

+0

我猜不是?你到底在說什麼? – user3831097 2015-02-09 01:35:27

回答

0

我不知道你在你的代碼做的究竟是什麼,但我已經寫了一個小功能做你想要什麼

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 

然後

command.ExecuteNonQuery() 

最後但並非最不重要的一點,請記住在你的SQL語句中使用參數化,因爲它會遵循良好的pra並且防止諸如SQL注入之類的安全問題。這就是OleDbParameter的用途。檢查下面的MSDN頁

OleDbCommand.Parameters Property

相關問題