DataSet更新数据库问题 [英] DataSet updating the database problem
问题描述
我一直在互联网上搜索几周,仍然无法找到问题的答案。
我有一个动态创建表单的程序,表单上的文本框自动绑定到数据库表的列(字段)。据我所知,存储数据的DataSet跟踪编辑,添加和删除该数据集中的所有记录。
这是我无法弄清楚的:我想采取修改后的DataSet并将对该数据集所做的更改写回数据库中的正确表。我通过DataSet更新数据库的所有示例都是通过硬连线代码完成的,而不是通过多功能数据绑定形式完成的。
这应该相当简单。打开与数据库的连接,获取修改后的DataSet并将更改写回数据库,然后关闭连接。
有谁知道在哪里可以找到这样的例子?或至少有一些如何做的例子?
谢谢,
MRM256
I have been searching the Internet off and on for several weeks and still cannot find an answer to my problem.
I have a program that dynamically creates a form where the textboxes on the form are automatically bound to the columns(fields) of the database table. As I understand it the DataSet where the data is stored tracks the editing, additions and deleting of all records in that data set.
Here is what I can't figure out: I want to take the modified DataSet and write the changes made on that data set back to the proper table in the database. All the examples I have seen for updating a database through the DataSet are done through hardwired code, not from a versatile data bound form.
This should be fairly simple. Open a connection to the database, take the modified DataSet and write the changes back to the database, then close the connection.
Does anyone know where to find such an example? Or at least some example on how to do it?
Thanks,
MRM256
推荐答案
在C#中将数据集更改保存到数据库 [ ^ ]
感谢Bonnie Dewitt(Bennett)解决了这个问题。她指出了Microsoft文档中没有关于如何使用DataSet的两个项目。
回顾一下:我的应用程序使用TableLayoutPanel,BindingNavigator和BindingSource创建数据输入表单。因为一切都是动态生成的; BindingNavigator上的Save按钮必须知道你正在使用哪个标签页,所以它可以更新正确的表格。
这是代码:
I have solved this problem thanks to Bonnie Dewitt(Bennett). She pointed out two items that were not in the Microsoft documentation on how to use DataSets.
To recap: My Application creates data entry forms using a TableLayoutPanel, a BindingNavigator, and a BindingSource. Since everything is generated dynamically; the Save button on the BindingNavigator had to know which tab page you were using so it could update the proper table.
Here is the code:
Private Sub BindingNavigatorSaveItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim strTab As String
Dim strSQL As String
Dim da As SqlDataAdapter
Dim sb As SqlCommandBuilder
Dim Msg As String
strTab = ctrlTab.SelectedTab.Name
'Get the table information using a Trusted connection
If chkTrusted.Checked Then
strCnn = cnnSQLSvrTrusted(cboSvrName.Text, cboDB.Text)
End If
'Get the table information through standard security method
If chkStandard.Checked Then
strCnn = cnnSQLSvrStandard(cboSvrName.Text, cboDB.Text, _
txtUser.Text, txtPW.Text)
End If
'Search the current TabPage for a BindingNavigator control
For Each c In Me.ctrlTab.Controls.Item(strTab).Controls
Dim oNav As BindingNavigator = TryCast(c, BindingNavigator)
'If the control is a BindingNavigator
If oNav IsNot Nothing Then
oNav.BindingSource.EndEdit()
Exit For
End If
Next
'Connect to the database
Using objConn As New SqlConnection(strCnn)
Try
If objConn.State = ConnectionState.Open Then
objConn.Close()
Else
objConn.Open()
strSQL = "SELECT * FROM " & strTab
da = New SqlDataAdapter(strSQL, objConn)
'Build Commands for add, delete, and update
sb = New SqlCommandBuilder(da)
'Update the selected table
da.Update(dsTbls.Tables(strTab))
Msg = strTab & " has been successfully updated."
MessageBox.Show(Msg)
End If
Catch ex As Exception
Msg = strTab & " not updated." & vbCrLf & "Exception: " & ex.Message
MessageBox.Show(Msg)
Finally
objConn.Close()
End Try
End Using
End Sub
谢谢,
MRM256
Thanks,
MRM256
这篇关于DataSet更新数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!