VB.net 2010数据集更新但访问数据库保持不变

时间:2014-01-18 09:59:37

标签: vb.net

我尝试从insert or update表单vb.netMS-Access数据库。

数据集更新但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

1 个答案:

答案 0 :(得分:0)

它已经有一段时间但我想我记得Access有点挑剔的提交。试试这个:

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

Trans.Commit()