2014-01-18 88 views
0

我從vb.net形式試圖insert or updateMS-Access數據庫。VB.net 2010集更新,但訪問數據庫保持相同

數據集的更新,但access數據庫的習慣。以下是我的代碼。

Try 
      Dim addLocation As String = "Insert into Provider (StateCode, Provider)" _ 
      & "values ('" & ComboBox1.Text & "', '" & TextBox2.Text & "')" 
      Dim sqlcommand As New OleDbCommand 
      conn.Open() 
      With sqlcommand 
       .CommandText = addLocation 
       .Connection = conn 
       .ExecuteNonQuery() 
      End With 
      MsgBox("One record added", MsgBoxStyle.Information) 
      refreshGrid() 

     Catch ex As Exception 
      MsgBox(ex.ToString) 
     End Try 

    Private Sub refreshGrid() 
     cnString = "PROVIDER = Microsoft.ace.oledb.12.0;data source =" & Application.StartupPath & "\HCHPClosedIn.accdb" 
     sqlQRY = "SELECT * FROM Provider" 
     conn = New OleDbConnection(cnString) 
     Try 
     conn.Open() 
     da = New OleDbDataAdapter(sqlQRY, conn) 
     Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da) 
     da.Fill(ds, "Customers") 
     DataGridView1.DataSource = ds 
     DataGridView1.DataMember = "Customers" 
     End Try 
    End Sub 
+0

你會得到什麼錯誤? –

+0

我沒有收到任何錯誤..它只是不添加到數據庫。數據集更新,因爲我在datagridview中使用它。 – Joseph

+0

郵你refreshgrid代碼 –

回答

0

它已經有一段時間了,但我想我記得Access對提交有點挑剔。試試這個:

With sqlcommand 
    .CommandText = addLocation 
    .Connection = conn 
    .ExecuteNonQuery() 
    .transaction = trans 
End With 

Trans.Commit()