如何从修改后的数据表更新 SQL 表?
我使用数据集设计器创建 FTWDataSet,其中包含 SQLExpress 数据库中的 AlarmText 表。到目前为止,我的表单仅包含 Datagridview1。下面的代码成功显示了 AlarmText 表的内容以及添加的一个复选框列(我将用仅显示数据填充该列,这不是问题)。
Dim ta As New FTWDataSetTableAdapters.AlarmTextTableAdapter
Dim dt As New FTWDataSet.AlarmTextDataTable
ta.Fill(dt)
DataGridView1.DataSource = dt
'create a new Bool column in the datatable
dt.Columns.Add("NewCol", (New Boolean).GetType)
要使用 DataGridView 编辑和保存 AlarmText 表中的值,我还需要做什么?
I used the DataSet Designer to create FTWDataSet which holds the AlarmText table from a SQLExpress database. This far my form contains ONLY Datagridview1. The code below successfully shows the contents of the AlarmText table plus the one added checkbox column (which I will populate with display-only data, and is not an issue here).
Dim ta As New FTWDataSetTableAdapters.AlarmTextTableAdapter
Dim dt As New FTWDataSet.AlarmTextDataTable
ta.Fill(dt)
DataGridView1.DataSource = dt
'create a new Bool column in the datatable
dt.Columns.Add("NewCol", (New Boolean).GetType)
What else do I need to do to use the DataGridView to edit and save values in the AlarmText table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是关于此主题的简短 MSDN 演练。
一些注意事项:
数据集
,并且您从除简单表或视图之外的任何内容中获取原始数据,那么您的适配器将不知道如何更新原始数据库中的任何内容。在这种情况下,您必须手动配置UPDATE
命令。有关参考,请参阅上述链接中的 TableAdapter 更新命令部分。我还应该提到的是,我像躲避瘟疫一样避免使用 ADO.Net 中的 TableAdapter。理论上它们非常方便且功能强大。在实践中,其中许多(尤其是 Oracle 提供商的)都是有缺陷的,如果它们不能完全正常工作,你就彻底完蛋了。
如果您仅从一个基础表中提取数据,则适配器应该可以正常工作(因此暂时忽略我的虚无建议)。在代码中添加附加列可能会破坏适配器(因为数据库表中没有相应的列)。
Here's a brief MSDN walkthrough on this topic.
Some notes:
Dataset
using the Dataset Designer, and you're fetching your original data from anything more than a simple table or view, then your adapter won't know how to update anything in the original database. You have to manually configure theUPDATE
command in this situation. For reference, see the TableAdapter Update Commands section in the above link.I should also mention that I avoid TableAdapters in ADO.Net like the plague. In theory they're very convenient and powerful. In practice, many of them (especially the one for the Oracle provider) are buggy, and if they don't work exactly right you're totally screwed.
If you're pulling from just one underlying table, the adapter should work fine (so ignore my nihilistic advice for the time being). It may be that adding an additional column in code will breaks the adapter (since there's no corresponding column in the database table).