我正在使用一个使用 sql 服务器数据库的应用程序。我试图在表格中插入一行,如下所示。此表有一个主键"prodNum"。它是自动生成的密钥。
当我尝试在表格中插入一行时,如下所示,在第 intResult = oSglProdTableAdapt.Update(oCableRecDataSet, "ProdTable")
行中出现异常:
{"列'prodNum'是只读的。
如您所见,在创建数据行时,我什至没有为"prodNum"列设置值。但是更新函数也尝试写入该列的值。 我尝试在sql管理工作室软件中使用插入查询进行插入。那里工作正常。所以我假设问题出在我的代码上。我错过了什么吗?
' a record does not exist for the new Cable Product
' therefore insert a new data row in the ProdTable table
' clear the DataSet
oCableRecDataSet.Clear()
' create the Data Row
Dim oDR As DataRow = oCableRecDataSet.Tables("ProdTable").NewRow()
' populate the datarow with values
'oDR("ProductIDNum") = 102
oDR("ProductID") = ProductID
oDR("DefinedDate") = DefinedDate
oDR("OperID") = OperID
oDR("CutsizeBased") = CutsizeBased
ProdTable.AddDRofProdTable(oDR)
' add the datarow to the dataset
oCableRecDataSet.Tables("ProdTable").Rows.Add(oDR)
' update the Database with values from Dataset with the Data adapter
intResult = oSglProdTableAdapt.Update(oCableRecDataSet, "ProdTable")
Public Sub AddDRofCableDef(ByRef oDR As DataRow)
oDR("Upper") = m_Upper
oDR("Spec") = m_Spec
oDR("IlUpper") = m_IlUpper
oDR("IlLower") = m_lIlLower
oDR("Spec") = m_Spec
oDR("MeanUpper") = m_MeanUpper
oDR("MeanLower") = m_MeanLower
oDR("MeanUL") = m_MeanUL
oDR("MeanLL") = m_MeanLL
oDR("SUL") = m_SUL
oDR("StartZone") = m_StartZone
End Sub
堆栈跟踪如下:
atSystem.Data.DataRow.set_Item(数据列,对象值( at System.Data.DataRow.set_Item(字符串列名称、对象值( at ACMS。DBCableRecordsCommand.OnRowUpdate(Object sender, OleDbRowUpdateEventArgs args( in D:\Software\clsDBCableRecordsCommand.vb:line 956 at System.Data.OleDb.OleDbRowUpdateEventHandler.Invoke(Object sender, OleDbRowUpdateEventArgs e( at System.Data.OleDb.OleDbDataAdapter.OnRowUpdate(RowUpdatedEventArgs 值( 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(DataSet dataSet, String srcTable( at ACMS。DBCableRecordsCommand.insertCableRecord(DBCableRecords Cable( in D:\Software\clsDBCableRecordsCommand.vb:line 251
更新 当我检查生成的数据行时,我注意到的一件事是计算了 ProdNum(主键(,即使我没有初始化该值。这可能是sql server不接受的原因。如何停止数据行生成主键?
我假设"oCableRecDataSet"是你的数据集的名称,而数据集只包含一个表,所以你可以试试这个:
oCableRecDataSet.Tables(0).Columns("produNum").ReadOnly = false
intResult = oSglProdTableAdapt.Update(oCableRecDataSet, "ProdTable")