edit.Click Cn.Open() CMDB </跨度> =新的SqlCommandBuilder(da) da.Update(ds,“vehicule”) Cn.Close()结束子
****这里是我用来更新数据库的代码
Private Sub锟edit edit_Click(sender As Object,e As EventArgs)处理edit.Click 如果ComboBox3.SelectedItem =“vehicule”那么 Cn.Open() CMDB </跨度> =新的SqlCommandBuilder(da) da.Update(ds,“vehicule锟
这将做你想要的。
Imports System.Data.SqlClient Public Class Form1 Dim sCommand As SqlCommand Dim sAdapter As SqlDataAdapter Dim sBuilder As SqlCommandBuilder Dim sDs As DataSet Dim sTable As DataTable Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True" Dim sql As String = "SELECT * FROM Stores" Dim connection As New SqlConnection(connectionString) connection.Open() sCommand = New SqlCommand(sql, connection) sAdapter = New SqlDataAdapter(sCommand) sBuilder = New SqlCommandBuilder(sAdapter) sDs = New DataSet() sAdapter.Fill(sDs, "Stores") sTable = sDs.Tables("Stores") connection.Close() DataGridView1.DataSource = sDs.Tables("Stores") DataGridView1.ReadOnly = True save_btn.Enabled = False DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect End Sub Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click DataGridView1.[ReadOnly] = False save_btn.Enabled = True new_btn.Enabled = False delete_btn.Enabled = False End Sub Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index) sAdapter.Update(sTable) End If End Sub Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click sAdapter.Update(sTable) DataGridView1.[ReadOnly] = True save_btn.Enabled = False new_btn.Enabled = True delete_btn.Enabled = True End Sub End Class
http://vb.net-informations.com/datagridview/vb.net_datagridview_database.htm
你只是太过分了。把事情简单化。您绑定到单个表。您只需要一个select语句,并在末尾添加所选的表名。单个DataAdapter和DataTable。 DataTable可以更新数据库,而无需再次通过ComboBox舞蹈。
使用块将确保您不仅关闭连接而且将其处理掉。
Private dt As DataTable Private da As SqlDataAdapter '**EDIT** move declare of rq to class level Private rq As String Private Sub ComboBox3_SelectedvalueChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedValueChanged '**EDIT** moved declaration to Class level rq = "select * from [cutting software].dbo." & ComboBox3.SelectedItem.ToString dt = New DataTable Using cn As New SqlConnection("Your connection string") da = New SqlDataAdapter(rq, cn) da.Fill(dt) 'If the connection is closed, the DataAdapter will open and close it for you End Using DataGridView1.DataSource = dt End Sub Private Sub edit_Click(sender As Object, e As EventArgs) Handles edit.Click Dim cmdb As New SqlCommandBuilder(da) Using cn As New SqlConnection("Your connection string") '**EDIT** Add line to re-establish connection for adapter da.SelectCommand = New SqlCommand(rq, cn) da.Update(dt) End Using End Sub
的 编辑 强> 重新建立DataAdapter的连接。