VB 2008 - SQL Server 2005 - DataGridView - 更新按钮 - 将更改写入数据库 [英] VB 2008 - SQL Server 2005 - DataGridView - update button - write changes to database
问题描述
我是vb的新手。
目前,我使用SQL Server 2005数据库中的表格中的数据填充DataGridView1。
我需要能够回写所有更改,只需点击 btnupdate
这个网站处理具体情况,并不是所有的代码都被VS在我的情况下接受,所以他们并不真正有用。
所以:
Private Sub btnupdate_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnupdate.Click
'这需要将更改保存到数据库'
End Sub
我可以插入一个简单的代码片段? / p>
谢谢。
主题很广泛。以下是使用DataTable和DataAdapter的示例。 DataTable用于保存数据库中的数据,并在绑定到网格时跟踪用户对特定行进行的更改。适配器生成执行更新和删除命令所需的sql,只要有效的SQL SELECT。有许多不同的方式来完成你想要的,但是,这是你可以做的一个例子。在看下面的代码之后,您可能需要深入了解具体的课程,以了解更多关于它们的内容,并根据您的需要进行调整。
注意:您的连接字符串和您的案例中的变量名称可能与此列表中显示的不同:
导入系统。 Data.SqlClient
公共类Form1
Dim connetionString As String
Dim连接作为SqlConnection
Dim适配器作为SqlDataAdapter
Dim cmdBuilder As SqlCommandBuilder
Dim ds As New DataSet
Dim change As DataSet
Dim sql As String
Dim i As Int32
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs )Handles Button1.Click
connetionString =Data Source = ServerName; Initial Catalog = DatabaseName; User ID = UserName; Password = Password
connection = New SqlConnection(connetionString)
sql =select *从产品
尝试
connection.Open()
adapter = New SqlDataAdapter(sql,connection)
adapter.Fill(ds)
connection.Close()
DataGridView1.DataSource = ds.Tables 0)
Catch ex As Exception
MsgBox(ex.ToString)
End尝试
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object ,ByVal e As System.EventArgs)Handles Button2.Click
'***由datagridview控件中的用户进行更新。
尝试
cmdBuilder =新的SqlCommandBuilder(适配器)
changes = ds.GetChanges()
如果更改IsNot Nothing然后
adapter.Update(更改)
结束If
MsgBox(Changes Done)
Catch ex As Exception
MsgBox(ex.ToString)
End尝试
End Sub
结束类
从源代码改编: http://vb.net-informations.com/dataadapter/dataadapter-datagridview-sqlserver.htm
I'm new to vb.
Currently, I'm populating 'DataGridView1' with data from a table in a SQL Server 2005 database.
I need to be able to write back all changes made by simply clicking btnupdate
Related threads on this site handle specific cases and not all code is accepted by VS in my case so they're not really helpful.
So:
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
'This needs to save the changes to the database'
End Sub
Is there a simple piece of code that I can insert?
thanks.
The topic is broad. Below is a sample using DataTable and DataAdapter. DataTable is used to hold data from database and track changes made by user to specific rows when bound to the grid. The adapter generates the sql required to carry out commands of update and delete provided a valid SQL SELECT. There are many different ways to accomplish what you want, however, this is a sample of what you can do. After taking a look to the code below, you may want to dig deeper in specific classes to learn more about them and adapt them to your needs.
Note: Your connection string and variable names in your case may vary from what is shown in this listing:
Imports System.Data.SqlClient
Public Class Form1
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As SqlDataAdapter
Dim cmdBuilder As SqlCommandBuilder
Dim ds As New DataSet
Dim changes As DataSet
Dim sql As String
Dim i As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
connection = New SqlConnection(connetionString)
sql = "select * from Product"
Try
connection.Open()
adapter = New SqlDataAdapter(sql, connection)
adapter.Fill(ds)
connection.Close()
DataGridView1.DataSource = ds.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'*** Process updates as made by the user in the datagridview control.
Try
cmdBuilder = New SqlCommandBuilder(adapter)
changes = ds.GetChanges()
If changes IsNot Nothing Then
adapter.Update(changes)
End If
MsgBox("Changes Done")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Adapted from source: http://vb.net-informations.com/dataadapter/dataadapter-datagridview-sqlserver.htm
这篇关于VB 2008 - SQL Server 2005 - DataGridView - 更新按钮 - 将更改写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!