C# sql update - InvalidOperationException

发布于 2025-01-08 11:57:26 字数 1489 浏览 4 评论 0原文

我正在开发一个需要数据库的项目。
我在 Youtube 上找到了一个教程,它演示了如何使用 SQL Server 2005 连接和编辑数据。
但是当我尝试时,我收到此错误InvalidOperationException

这是整个编辑代码

con.Open();

DataTable dt = new DataTable();
//load all records from sample table
SqlDataAdapter da = new SqlDataAdapter("select * from sampleEdit where ID=" +
    textBox1.Text + " ", con);
da.Fill(dt);

//start the editing of the selected record
dt.Rows[0].BeginEdit();

dt.Rows[0][1] = textBox2.Text;

//stop the editing
dt.Rows[0].EndEdit();

//declare the sql commandbuilder that allow saving of records
SqlCommandBuilder cb = new SqlCommandBuilder(da);

//update the database
da.Update(dt);

//close the connection
con.Close();

//call the method that display the record to the gridview
displayRecords();

错误显示在更新部分。
应该是什么问题呢?

这是完整的异常错误

System.InvalidOperationException: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.  
at System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgs rowUpdatedEvent, DataRow dataRow)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at DatabaseConnect3.Form1.btnEdit_Click(Object sender, EventArgs e)

I am working on a project that needs a database.
I have found a tutorial which I've seen in Youtube, and it demonstrates how to connect and edit data with SQL Server 2005.
But when I tried it, I receive this error InvalidOperationException

This is the whole edit code

con.Open();

DataTable dt = new DataTable();
//load all records from sample table
SqlDataAdapter da = new SqlDataAdapter("select * from sampleEdit where ID=" +
    textBox1.Text + " ", con);
da.Fill(dt);

//start the editing of the selected record
dt.Rows[0].BeginEdit();

dt.Rows[0][1] = textBox2.Text;

//stop the editing
dt.Rows[0].EndEdit();

//declare the sql commandbuilder that allow saving of records
SqlCommandBuilder cb = new SqlCommandBuilder(da);

//update the database
da.Update(dt);

//close the connection
con.Close();

//call the method that display the record to the gridview
displayRecords();

The error shows at the update part.
What should be the problem?

this was the full exception error

System.InvalidOperationException: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.  
at System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgs rowUpdatedEvent, DataRow dataRow)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at DatabaseConnect3.Form1.btnEdit_Click(Object sender, EventArgs e)

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

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

发布评论

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

评论(1

静待花开 2025-01-15 11:57:26

您的源表无效,因为您很可能没有在表上定义主键。只需将 ID 列设置为表的主键,代码就可以工作。

Your source table is invalid because you most likely didn't define a primary key on the table. Just make the ID column the primary key of the table and the code should work.

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