数据集何时应反映在 vb.net 中插入后更新的行数
我有一个 SQL 数据库,并为其中的示例表创建了一个数据集。
数据集是 HotelDataSet2,要插入其中的数据表适配器是Various_itemsTableAdapter。
MsgBox(HotelDataSet2.various_items.Rows.Count)
Various_itemsTableAdapter.Insert(Item_nameTextBox.Text, itemcode, PackingTextBox.Text, UomTextBox.Text, PriceTextBox.Text, RemarksTextBox.Text, TaxTextBox.Text, StatusCheckBox.Checked, Rate_inclusiveCheckBox.Checked)
MsgBox(HotelDataSet2.various_items.Rows.Count)
在插入之前和之后,这总是反映相同的计数。但是如果我执行此操作,
MsgBox(HotelDataSet2.various_items.Rows.Count)
Various_itemsTableAdapter.Insert(Item_nameTextBox.Text, itemcode, PackingTextBox.Text, UomTextBox.Text, PriceTextBox.Text, RemarksTextBox.Text, TaxTextBox.Text, StatusCheckBox.Checked, Rate_inclusiveCheckBox.Checked)
Various_itemsTableAdapter.Fill(HotelDataSet2.various_items)
MsgBox(HotelDataSet2.various_items.Rows.Count)
它会显示新计数为旧计数的+1。所以我得出的结论是,每次我通过表适配器将一些数据更改到表中时,我都必须重新填充数据集?那有什么用呢??
i have a sql database, and have created a dataset for a sample table in it.
The dataset is HotelDataSet2 and data table adapter to insert to it is Various_itemsTableAdapter.
MsgBox(HotelDataSet2.various_items.Rows.Count)
Various_itemsTableAdapter.Insert(Item_nameTextBox.Text, itemcode, PackingTextBox.Text, UomTextBox.Text, PriceTextBox.Text, RemarksTextBox.Text, TaxTextBox.Text, StatusCheckBox.Checked, Rate_inclusiveCheckBox.Checked)
MsgBox(HotelDataSet2.various_items.Rows.Count)
This always reflects the same count, before and after inserting..But if i execute this
MsgBox(HotelDataSet2.various_items.Rows.Count)
Various_itemsTableAdapter.Insert(Item_nameTextBox.Text, itemcode, PackingTextBox.Text, UomTextBox.Text, PriceTextBox.Text, RemarksTextBox.Text, TaxTextBox.Text, StatusCheckBox.Checked, Rate_inclusiveCheckBox.Checked)
Various_itemsTableAdapter.Fill(HotelDataSet2.various_items)
MsgBox(HotelDataSet2.various_items.Rows.Count)
it shows the new count to be +1 of the old one. So i concluded that everytime i change some data to the table via table adapter, i have to always refill the dataset?? How is that useful then??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的处理方式是错误的。您应该使用
DataSet
进行更改(即,创建DataRow
及其表并将这些行插入表中),然后传递适当的DataTable
从DataSet
到TableAdapter
,以便通过调用Update
函数将更改保存到数据库>TableAdapter直接调用
Insert
函数只会将该数据插入到数据库本身的表中;它对数据的任何本地副本(例如您的DataSet
)没有任何影响。顺便说一句,尽量避免使用特定于语言的函数,例如
MsgBox
。相反,要显示消息框,请尝试调用MessageBox.Show(...)
,因为这是与语言无关的。编辑
澄清一下,执行此操作的正确方法是创建一个新的
DataRow
,用新值填充其列,然后将该DataRow
添加到相应的表,然后通过Update
函数将该表传递给TableAdapter
。例如,我...
要插入包含这些值的行,我会这样做:
You're going about this the wrong way. You should be using the
DataSet
to make your changes (ie, createDataRow
's with its tables and insert those rows into the table), then pass the appropriateDataTable
from theDataSet
to yourTableAdapter
in order to persist the changes to the database by calling theUpdate
function on theTableAdapter
Calling the
Insert
function directly only inserts that data into the table in the database itself; it does not have any effect on any local copies of the data (such as yourDataSet
).As a side note, try to avoid using language-specific functions like
MsgBox
. Instead, to show a message box try callingMessageBox.Show(...)
, as that is language-independent.Edit
To clarify, the proper way to do this is to create a new
DataRow
, populate its columns with the new values, add thatDataRow
to the appropriate table, then pass the table to theTableAdapter
through theUpdate
function.For example, I have...
To insert a row with those values, I would do this: