数据集何时应反映在 vb.net 中插入后更新的行数

发布于 2024-08-03 10:59:07 字数 956 浏览 10 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-08-10 10:59:07

你的处理方式是错误的。您应该使用 DataSet 进行更改(即,创建 DataRow 及其表并将这些行插入表中),然后传递适当的 DataTableDataSetTableAdapter,以便通过调用 Update 函数将更改保存到数据库>TableAdapter

tableAdapter.Update(dataSetName);

直接调用Insert 函数只会将该数据插入到数据库本身的表中;它对数据的任何本地副本(例如您的DataSet)没有任何影响。

顺便说一句,尽量避免使用特定于语言的函数,例如 MsgBox。相反,要显示消息框,请尝试调用 MessageBox.Show(...),因为这是与语言无关的。

编辑

澄清一下,执行此操作的正确方法是创建一个新的 DataRow,用新值填充其列,然后将该 DataRow 添加到相应的表,然后通过 Update 函数将该表传递给 TableAdapter

例如,我...

A DataSet named myDataSet
A DataTable in myDataSet called MyTable
A TableAdapter for MyTable called myTableAdapter
Two columns in that table, FirstName and LastName
Two TextBoxes called txtFirstName and txtLastName

要插入包含这些值的行,我会这样做:

DataRow row = myDataSet.MyTable.NewRow(); // creates a new row with the correct columns

row["FirstName"] = txtFirstName.Text;
row["LastName"] = txtLastName.Text;

myDataSet.MyTable.Rows.Add(row); // adds the new row to the in-memory table

myTableAdapter.Update(myDataSet); // persists all of the changes to the DataSet

You're going about this the wrong way. You should be using the DataSet to make your changes (ie, create DataRow's with its tables and insert those rows into the table), then pass the appropriate DataTable from the DataSet to your TableAdapter in order to persist the changes to the database by calling the Update function on the TableAdapter

tableAdapter.Update(dataSetName);

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 your DataSet).

As a side note, try to avoid using language-specific functions like MsgBox. Instead, to show a message box try calling MessageBox.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 that DataRow to the appropriate table, then pass the table to the TableAdapter through the Update function.

For example, I have...

A DataSet named myDataSet
A DataTable in myDataSet called MyTable
A TableAdapter for MyTable called myTableAdapter
Two columns in that table, FirstName and LastName
Two TextBoxes called txtFirstName and txtLastName

To insert a row with those values, I would do this:

DataRow row = myDataSet.MyTable.NewRow(); // creates a new row with the correct columns

row["FirstName"] = txtFirstName.Text;
row["LastName"] = txtLastName.Text;

myDataSet.MyTable.Rows.Add(row); // adds the new row to the in-memory table

myTableAdapter.Update(myDataSet); // persists all of the changes to the DataSet
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文