仅更新数据表中的一行 [英] Update Only One Row in a DataTable
问题描述
我有一个带有datagridview控件的表格,操作员可以在其中更改或不更改单元格中的值.如果有任何数据更改,我想使用已更新的行来更新数据库.在下面使用的UpdateRow函数中,接收到的参数intDetailID是要更新的表中的唯一键.我正在使用以下代码来尝试执行此操作.尽管我没有收到任何错误,但数据库尚未更新.我想念什么?
I have a form with a datagridview control where an operator may or may not change the value in the cells. If any data is changed, I want to update the database with the updated row. In the UpdateRow function I''m using below, the argument received, intDetailID, is a unique key in the table to be updated. I''m using the following code to try to do this. Although I''m not getting any errors, the database is not being updated. What am I missing?
Private Sub UpdateRow(ByVal intDetailID As Integer)
''Define data adapter for only one row data table
Dim daPhysCME_Update As New OleDb.OleDbDataAdapter
Dim daPhysCMEUpdateSql As String
daPhysCMEUpdateSql = "Select * from tbl_PhysCME WHERE DetailID=" & intDetailID
daPhysCME_Update = New OleDb.OleDbDataAdapter(daPhysCMEUpdateSql, Cnxn)
''Define and fill data table with only one row to be updated
Dim dtPhysCME_Update As New DataTable
daPhysCME_Update.Fill(dtPhysCME_Update)
cbCommandBuilder = New OleDb.OleDbCommandBuilder(daPhysCME_Update)
''Populate the table with new values
dtPhysCME_Update.Rows(0).Item("DetailID") = aftDetailID
dtPhysCME_Update.Rows(0).Item("PhysicianID") = aftPhysicianID
dtPhysCME_Update.Rows(0).Item("Start Date") = aftStartDate
dtPhysCME_Update.Rows(0).Item("End Date") = aftEndDate
dtPhysCME_Update.Rows(0).Item("Subject") = aftSubject
dtPhysCME_Update.Rows(0).Item("Hours") = aftHours
dtPhysCME_Update.Rows(0).Item("Category") = aftCategory
dtPhysCME_Update.Rows(0).Item("Sponsor") = aftSponsor
dtPhysCME_Update.AcceptChanges()
''Update the database
daPhysCME_Update.Update(dtPhysCME_Update)
End Sub
我有更多信息可以帮助您解决此问题.我在".Update"语句之前添加了以下代码:
I have some more information to help assist in solving this problem. I added the following code just before the ".Update" statement:
Debug.Print(cbCommandBuilder.GetUpdateCommand.CommandText)
这是我得到的第一部分:
更新tbl_PhysCME SET PhysicianID =?,开始日期=?,结束日期=?,主题=?,小时=?,类别=? ...
所有列值都是问号是否重要?
还有其他想法吗?
[/Edit]
This is the first part of what I got:
UPDATE tbl_PhysCME SET PhysicianID = ?, Start Date = ?, End Date = ?, Subject = ?, Hours = ?, Category = ? ...
Is the fact that all the column values are question marks significant?
Any other idea?
[/Edit]
推荐答案
如果DetailID
是身份或主键,则可能是问题的原因.
请参阅此MSDN页面上的自动生成的命令规则"部分. [ ^ ].
IfDetailID
is an Identity or a Primary Key, that might be the cause of the problem.
See the Rules for Automatically Generated Commands section on This MSDN Page[^].
这篇关于仅更新数据表中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!