使用 Tableadapter 插入相同记录作为副本的快速方法
我有一个包含一堆字段的数据表,并且由表适配器填充数据。通常,需要插入的记录有点不同(只有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试复制行:
Try this to copy row:
接受的答案对我不起作用,因为 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?