1

我的目标是从数据库中取出表,修改并将其放回同一个地方。

我决定使用 oleDbDataAdapter.Fill/.Update 例程来防止逐个单元格的更新并加快进程。将表从 mdb.DataBase 读取到 DataTable 是成功的,并且修改成功,但是我更新数据库失败了。

我可以假设我遇到麻烦的两种方式:1. adapter.UpdateCommand 错误 2. 在调用 adapter.update(datatable) 之前遗漏了(一些)关键参数

固定点 1

  • 我已将主键包含在选择查询中
  • 我尝试使用 dbCommandBuilder.GetUpdateCommand()
  • 我试图从 dbCommandBuilder 属性中复制更新字符串并将其用作 sting 参数失败。

在固定点 2

我已经实现了在循环中使用 UpdateCommand.Parameters 和直接填充目标数据库的选项,逐行逐个元素地填充。但我试图避免它并立即更新整个表。

你能解释一下我想念什么吗?

这是一个代码示例

                    Dim connetionString As String
                    Dim oledbCnn As OleDbConnection
                    Dim oledbAdapter As OleDbDataAdapter
                    Dim sssql, susql As String

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sMDBFile & ";"
sssql = "SELECT MAPINFO_ID, " & SFieldName & ", AB_P_PIND, AB_P_CITY, " _
  & "AB_P_DISTR, AB_P_SDISTR, AB_P_STR, AB_P_NUM, AB_P_BLD, AB_P_LIT, AB_P_XTRA, " _
  & "AB_P_XEPT, AB_UINF, AB_P_DONE FROM " & DBTableName
susql = "UPDATE " & DBTableName & " SET AB_P_PIND = @AB_P_PIND, AB_P_CITY = @AB_P_CITY, " _
  & "AB_P_DISTR = @AB_P_DISTR, AB_P_SDISTR = @AB_P_SDISTR, AB_P_STR = @AB_P_STR, " _
  & "AB_P_NUM = @AB_P_NUM, AB_P_BLD = @AB_P_BLD, AB_P_LIT = @AB_P_LIT, AB_P_XTRA = @AB_P_XTRA, " _
  & "AB_P_XEPT = @AB_P_XEPT, AB_UINF = @AB_UINF, AB_P_DONE = @AB_P_DONE WHERE MAPINFO_ID = @MAPINFO_ID"
'susql = "UPDATE TabVer1 SET Адрес = ?, AB_P_PIND = ?, AB_P_CITY = ?, AB_P_DISTR = ?, AB_P_SDISTR = ?, " _
'  & "AB_P_STR = ?, AB_P_NUM = ?, AB_P_BLD = ?, AB_P_LIT = ?, AB_P_XTRA = ?, AB_P_XEPT = ?, " _
'  & "AB_UINF = ?, AB_P_DONE = ? WHERE ((MAPINFO_ID = ?) AND ((? = 1 AND Адрес IS NULL) OR " _
'  & "(Адрес = ?)) AND ((? = 1 AND AB_P_PIND IS NULL) OR (AB_P_PIND = ?)) AND " _
'  & "((? = 1 AND AB_P_CITY IS NULL) OR (AB_P_CITY = ?)) AND ((? = 1 AND AB_P_DISTR IS NULL) " _
'  & "OR (AB_P_DISTR = ?)) AND ((? = 1 AND AB_P_SDISTR IS NULL) OR (AB_P_SDISTR = ?)) AND " _
'  & "((? = 1 AND AB_P_STR IS NULL) OR (AB_P_STR = ?)) AND ((? = 1 AND AB_P_NUM IS NULL) " _
'  & "OR (AB_P_NUM = ?)) AND ((? = 1 AND AB_P_BLD IS NULL) OR (AB_P_BLD = ?)) AND " _
'  & "((? = 1 AND AB_P_LIT IS NULL) OR (AB_P_LIT = ?)) AND ((? = 1 AND AB_P_XTRA IS NULL) OR " _
'  & "(AB_P_XTRA = ?)) AND ((? = 1 AND AB_P_XEPT IS NULL) OR (AB_P_XEPT = ?)) AND " _
'  & "((? = 1 AND AB_UINF IS NULL) OR (AB_UINF = ?)) AND ((? = 1 AND AB_P_DONE IS NULL) OR (AB_P_DONE = ?)))"
' second ver of sussql (under comments) is taken from dbCommandBuilder's properties                    


oledbCnn = New OleDbConnection(connetionString)
Try
   oledbCnn.Open()
   oledbAdapter = New OleDbDataAdapter
   dTable.PrimaryKey = New DataColumn() {dTable.Columns("MAPINFO_ID")}
      'Dim DbCommandBuilder As New OleDbCommandBuilder(oledbAdapter)
      ' here's some illustrations of different ways I've tried with update strings
   oledbAdapter.SelectCommand = New OleDbCommand(sssql, oledbCnn)
      'oledbAdapter.UpdateCommand = DbCommandBuilder.GetUpdateCommand()
   oledbAdapter.UpdateCommand = New OleDbCommand(susql, oledbCnn)

   oledbAdapter.Fill(dTable)

   ''' dTable modification

   dTable.AcceptChanges()
      ' DataTable updates 
   oledbAdapter.Update(dTable)
      ' Database does not
   oledbAdapter.Dispose()
   oledbCnn.Close()

Catch ex As Exception
   MsgBox("Can not open connection ! ")
End Try
4

1 回答 1

0

您的代码存在问题:使用参数来避免 sql 注入,使用Using...End Using一次性对象的格式,包括连接、适配器对象等。

对于眼前的问题,请尝试在更新之前不要调用 AcceptChanges。AcceptChanges 是一个标志,用于标记哪些行是脏的。

oledbAdapter.Update(dTable)
dTable.AcceptChanges()
于 2012-09-04T19:27:42.743 回答