我有一个工作代码,它使用SQL命令从数据库中删除一个选择行,如果我在访问中使用该命令,该命令有效。但是,当我想用VB.NET中的delete
按钮执行此操作时,它不会保存它。当我检查程序是否已经消失时,它会工作并更新所有内容等等。当我退出程序并再次启动时,它会恢复正常。我需要帮助解决这个问题!
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 :(得分: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语句中使用参数化,因为它将遵循良好实践并防止SQL注入等安全问题。这是OleDbParameter
的用途。查看下面的MSDN页面了解更多信息