我已經通過Visual Studio嚮導使用vb格式的DataGridView綁定了sql數據庫表。
我嘗試了下面的代碼,它成功執行並顯示消息「已成功更新」,但實際上它不會對SQL數據庫進行任何更改。當我重新加載表單時,我沒有看到任何更改。我是VB.Net的新手,只有這件事阻止我完成我的項目。請幫助...在此先感謝如何通過點擊按鈕更新DataGridView中的數據來更新數據庫?
Imports System.Data.SqlClient
Public Class FormUpdate
'Variables declared here
Dim Con As New SqlConnection
Dim Cmd As New SqlCommand
Dim ds As DataSet
Dim da As New SqlDataAdapter
Dim cmdString As String
Dim dt As New DataTable
Dim dr As SqlDataReader
Dim cmdbl As New SqlCommandBuilder
Public Sub FormUpdate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ShipmentDataSetDataGridView.Shipment_Main' table. You can move, or remove it, as needed.
Me.Shipment_MainTableAdapter2.Fill(Me.ShipmentDataSetDataGridView.Shipment_Main)
'CONNECTION STRING FOR THE DATABASE
Con.ConnectionString = "Data Source=TARIQUE;Initial Catalog=shipment;Integrated Security=True"
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Try
Con.Open()
cmdbl = New SqlCommandBuilder(da)
da.Update(dt)
MessageBox.Show("Updated successfully!")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Con.Close()
End Sub
請把你用來初始化你的對象'da','dt'和'Con'的代碼貼出來。 –
@ S.AdamNissley添加代碼.... –
您的意思是這個代碼...這是我用來填充搜索數據網格視圖....「'通過搜索按鈕執行搜索操作 Private Sub btnSearch_Click(發送者爲對象,例如作爲EventArgs的)把手btnSearch.Click dt.Clear() DataGridView1.DataSource = DT DataGridView1.DataSource =無 如果txtSearch.Text =什麼然後 cmdString = 「SELECT * FROM Shipment_Main」 da =新的SqlDataAdapter(cmdString,Con) da.Fill(dt) DataGridView1.DataSource = dt' –