2012-09-04 107 views
1

我的目標是從數據庫中取出表格,修改並將其放回原處。oleDbDataAdapter更新字符串

我決定使用oleDbDataAdapter.Fill/.Update例程來防止逐個單元更新並加速進程。從mdb.DataBase中讀取表格到DataTable中是成功的,並且modyfying也可以,但是我更新數據庫失敗了。

我想從那裏我得到的煩惱兩種方式:1。 是adapter.UpdateCommand錯誤 2(一些)關鍵參數(S)錯過調用adapter.update之前(數據表)

雖然固定點1

  • 我已經包括了主鍵成選擇查詢
  • 我試圖用dbCommandBuilder.GetUpdateCommand()
  • 我試圖更新字符串從dbComman複製dBuilder屬性並將其用作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 

回答

0

有你的代碼的問題:使用的參數,以避免SQL注入,使用Using...End Using格式爲您一次性對象,包括連接,適配器對象等

對於直接的問題,請在更新之前嘗試不要調用AcceptChanges。 AcceptChanges是一個標記,標記哪些行是髒的。

oledbAdapter.Update(dTable) 
dTable.AcceptChanges() 
+0

感謝您使用...結束使用提醒。關於aceeptChanges之前的更新表:它導致豁免「一個或多個需要參數是mising」。 – user1646918