使用 Tableadapter 插入相同记录作为副本的快速方法

发布于 2024-12-18 11:19:49 字数 516 浏览 1 评论 0原文

我有一个包含一堆字段的数据表,并且由表适配器填充数据。通常,需要插入的记录有点不同(只有id)。我不希望用户再次重新输入所有内容,所以有没有一种快速方法将当前记录插入数据库。请注意,我所有的插入、更新和删除操作都基于我的表适配器。

//Get only one last record that has been added when the form load.
bookTableAdapter.FillByLastID(bookDataSet.dtBook);

private void btnNew_Click(object sender, EventArgs e)
{  
  bookBindingSource.AddNew();  
}

private void btnSave_Click(object sender, EventArgs e)
{
  this.Validate();
  bookBindingSource.EndEdit();
  bookTableAdapter.Update(bookDataSet.dtBook);
}

I have a datatable with a bunch of fields, and it's filled data by tableadapter. Usually, records that need to be inserted are a little bit different (only id). I don't want user to retypes everything again so, is there a quick way to insert current record into database. Notice that all my insert, update and delete operations are based on my tableadapter.

//Get only one last record that has been added when the form load.
bookTableAdapter.FillByLastID(bookDataSet.dtBook);

private void btnNew_Click(object sender, EventArgs e)
{  
  bookBindingSource.AddNew();  
}

private void btnSave_Click(object sender, EventArgs e)
{
  this.Validate();
  bookBindingSource.EndEdit();
  bookTableAdapter.Update(bookDataSet.dtBook);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

慕巷 2024-12-25 11:19:49

尝试复制行:

        DataRow row = table.Rows[0]; // row you want to copy

        DataRow newRow = table.NewRow();
        newRow.ItemArray = row.ItemArray.ToArray();
        newRow["Id"] = 10;  // change Id

        table.Rows.Add(newRow);

Try this to copy row:

        DataRow row = table.Rows[0]; // row you want to copy

        DataRow newRow = table.NewRow();
        newRow.ItemArray = row.ItemArray.ToArray();
        newRow["Id"] = 10;  // change Id

        table.Rows.Add(newRow);
亣腦蒛氧 2024-12-25 11:19:49

接受的答案对我不起作用,因为 TableAdapter 查询返回的列数比新行多,即 33。我的表有 32 列。这是遗留代码和数据库。

然而,这篇文章给了我答案: 尝试添加行时此行已属于另一个表错误?

The accepted answer didn't work for me because the TableAdapter query returned more columns than a new row had, 33. My table has 32 columns. This is legacy code and database.

However, this article gave me the answer: This Row already belongs to another table error when trying to add rows?

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