1
我正在處理需要數據庫的項目。
我發現我在YouTube中看到的教程,它演示瞭如何連接並使用SQL Server編輯數據2005
但是,當我嘗試了,我收到此錯誤InvalidOperationException
c#sql update - 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)
它有助於包含拋出的異常的全文。如果它是公開可見的,那麼連接Youtube視頻也無妨。 – Guvante 2012-02-24 17:02:27
@Guvante,對不起,我忘了鏈接。剛剛下載了視頻 – 2012-02-24 17:16:37