[英]Unable to save from DataGridView to SQL Server Database VB.Net
[英]VB.NET Save the delete command in access database in datagridview
我有使用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
我不確定您在代碼中到底在做什么,但是我寫了一個小函數來做您想要的
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頁面以獲取更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.