保存和关闭应用程序后VB数据库没有更新?

时间:2016-04-03 03:17:47

标签: vb.net visual-studio

当我输入新信息并保存时,我试图让我的程序更新数据库,其他所有内容都在我的应用程序中工作,但是当我重新加载应用程序时它没有保存我的更改。什么似乎是错误?

导入System.IO Imports System.Data.OleDb 公共类Form1     私有DBPath为字符串

Private conn As OleDbConnection
Private adapter As OleDbDataAdapter
Private dtMain As DataTable
Private dataSet As DataSet
Private cmdBuilder As OleDbCommandBuilder

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DBPath = Application.StartupPath + "\MEGACITIES.mdb"
    conn = New OleDbConnection(Convert.ToString("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") & DBPath)
    conn.Open()
    If Not File.Exists(DBPath) Then
        MessageBox.Show("Please copy the mdb to the path: " + Application.StartupPath)

    Else
        ' connect to DB        
        adapter = New OleDbDataAdapter("SELECT * FROM [Cities]", conn)
        Dim builder = New OleDbCommandBuilder(adapter)
        dataSet = New DataSet()
        adapter.Fill(dataSet, "Cities")
        DataGridView1.DataSource = dataSet.Tables("Cities")
    End If


End Sub

Private Sub btnPopulationByOrder_Click(sender As Object, e As EventArgs) Handles btnPopulationByOrder.Click

    adapter = New OleDbDataAdapter("SELECT * FROM [Cities] order by pop2015", conn)
    Dim builder = New OleDbCommandBuilder(adapter)
    'dtMain = New DataTable()
    'adapter.Fill(dtMain)
    'DataGridView1.DataSource = dtMain
    dataSet = New DataSet()
    adapter.Fill(dataSet, "Cities")
    DataGridView1.DataSource = dataSet.Tables("Cities")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    adapter = New OleDbDataAdapter("SELECT Cities.city,Cities.country,Cities.pop1995,Countries.currency FROM [Cities] inner join Countries on Cities.country = Countries.country", conn)
    Dim builder = New OleDbCommandBuilder(adapter)
    dataSet = New DataSet()
    adapter.Fill(dataSet, "Cities")
    DataGridView1.DataSource = dataSet.Tables("Cities")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If TextBox1.Text.Trim() = "" Then
        MessageBox.Show("Please enter the country name in text box.")
    Else

        adapter = New OleDbDataAdapter("SELECT city from [Cities] where country = '" + TextBox1.Text + "'", conn)
        Dim builder = New OleDbCommandBuilder(adapter)
        dataSet = New DataSet()
        adapter.Fill(dataSet, "Cities")
        DataGridView1.DataSource = dataSet.Tables("Cities")
    End If
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    cmdBuilder = New OleDbCommandBuilder(adapter)
    adapter.Update(dataSet, "Cities")
    DataGridView1.DataSource = dataSet.Tables("Cities")
    MessageBox.Show("1 changed Records were stored", "Project4Spring2016")

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

您必须对您尝试保存的记录执行更新sql语句。现在看来你只有选择陈述