【问题标题】:oleDbDataAdapter Update stringoleDbDataAdapter 更新字符串
【发布时间】:2012-09-05 03:27:42
【问题描述】:

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

我决定使用 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

【问题讨论】:

    标签: sql database vb.net


    【解决方案1】:

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

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

    oledbAdapter.Update(dTable)
    dTable.AcceptChanges()
    

    【讨论】:

    • 感谢使用...结束使用提醒。关于 aceeptChanges 之前的更新表:它会导致异常“缺少一个或多个必需参数”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2017-12-04
    相关资源
    最近更新 更多