2012-11-13 59 views
1

試圖讓這一點代碼輸入一個玩家的名字填入數據行,然後在數據庫中更新它。它目前在數據行更新,但我無法獲得更新數據庫的代碼。你能幫助歡呼嗎?VB.NET和數據庫

Using da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Team1", con) 
      da.Fill(ds, "BPAWA") 

      txtFirstName.Text = ds.Tables("BPAWA").Rows(0).Item(1) 

      T1P1 = InputBox("Name of Player 1") 

      ds.Tables("BPAWA").Rows(0).Item(1) = T1P1 

      ds.Tables("BPAWA").AcceptChanges() 
      ds.AcceptChanges() 

      da.Update(ds, "BPAWA") 
      MsgBox("Player 1 Added Successfully") 
     End Using 

回答

1

您OleDbDataAdapter的未鏈接到一個實際的命令沒有你似乎在代碼中創建的適配器。 See an example here

缺少這樣

Dim da As OleDbDataAdapter = new OleDbDataAdapter(selectCommand, connection); 

一條線,所以我可以用這種方式重寫代碼(避免全局變量)

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click 
    Dim dbProvider As String = "PROVIDER=microsoft.ace.oledb.12.0;" 
    Dim dbSource As String = "Data Source = C:\BP_Table_Project.accdb" 
    Dim ds as DataSet = new DataSet() 

    Using con As OleDbConnection = new OleDbConnection() 
     con.ConnectionString = dbProvider & dbSource 
     con.Open() 

     MsgBox("Connection With Database Established", 0, "Database Connection") 

     Using da as OleDbDataAdapter = new OleDbDataAdapter("SELECT * FROM Team1", con) 
      Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(da) 
      da.Fill(ds, "BPAWA") 
      ...... 

      da.Update(ds, "BPAWA") 
      MsgBox("Player 1 Added Successfully") 
     End Using 
     MsgBox("Connection With Database Closed", 0, "Database Connection") 
    End Using 
End Sub 
+0

您好史蒂夫我試過你了,它給我帶來了一大堆封裝錯誤? –

+0

用簡單的數據庫進行測試並更改了一些聲明。檢查更新的答案 – Steve

+0

嘿史蒂夫,感謝所有幫助到目前爲止。它現在運行,現在運行檢查的錯誤顯示數據集中的名稱已更改。雖然它改變了數據庫中的名字。我需要運行一個SQL命令嗎?問候 –

-1

之前da.Update(ds, "BPAWA")

地址:

ds.Tables("BPAWA").AcceptChanges() 
ds.AcceptChanges() 
+0

錯誤。如果您調用AcceptChanges,那麼RowState屬性將更改爲'Unchanged'並且不會將任何內容保存到數據庫 – Steve