如何从Visual Basic中的DataGrid视图更新Access数据库

问题描述:

我需要更新代码的帮助,它可以更新Microsoft Access数据库.我认为我的代码正确,但是当我进入Access时,没有数据,对此有什么想法吗?如果在文件中,那么我在哪里可以在Visual Basic中看到数据库?

I need help with updating my code to it can update the Microsoft Access Database. I think I got the code right but when I went into Access there was no data there any thoughts on that? If it's in the file then where can I see the database in visual Basic?

导入System.Data.OleDb

公共课程表格1

   昏暗的提供程序作为字符串
   昏暗的数据文件作为字符串
   昏暗的connString作为字符串
   作为新OleDbConnection的点心(提供程序= Microsoft.ACE.OLEDB.12.0;数据源= G:\ Music Session Database \ Music Database.accdb")
   将Dim ds作为新数据集

   昏暗的dt作为新的数据表

    Dim da As New OleDbDataAdapter

   私有Sub btnexit_Click(作为对象发送,作为EventArgs发送)处理btnexit.Click
       Me.Close()
   结束子



   私有Sub btnsubmit_Click(ByVal发送者为System.Object,ByVal e为EventArgs)处理btnsubmit1.Click

       Me.Music_DatabaseTableAdapter.Insert(Me.songTitle.Text,Me.songArtist.Text,Me.songAlbum.Text,Me.yearReleased.Text)
       Me.Music_DatabaseTableAdapter.Fill(Me.Music_DatabaseDataSet.Music_Database)


       con.Open()
       MsgBox(已添加记录")
       con.Close()
       songTitle.Text =""
       songArtist.Text =""
       songAlbum.Text =""
       yearReleased.Text =""


   结束子


   私有Sub btnsumbit2_Click(作为对象发送,作为EventArgs发送)处理btnsumbit2.Click

       Me.Play_SessionTableAdapter.Insert(Me.songTitle.Text,Me.songArtist.Text,Me.songAlbum.Text,Me.yearReleased.Text,Me.datePlayed.Text,Me.timePlayed.Text,Me.genre.Text)
       Me.Play_SessionTableAdapter.Fill(Me.Music_DatabaseDataSet.Play_Session)


       con.Open()
       MsgBox(已添加记录")
       con.Close()
       songTitle.Text =""
       songArtist.Text =""
       songAlbum.Text =""
       yearReleased.Text =""
       datePlayed.Text =""
       timePlayed.Text =""
       genre.Text =""



   结束子

   私有Sub btnsubmit3_Click(发送者作为对象,e作为EventArgs)处理btnsubmit3.Click
     

       Me.Song_Artist_InformationTableAdapter.Insert(Me.songArtist.Text,Me.genre.Text,Me.origin.Text,Me.artistInformation.Text)
       Me.Song_Artist_InformationTableAdapter.Fill(Me.Music_DatabaseDataSet.Song_Artist_Information)

       con.Open()
       MsgBox(已添加记录")
       con.Close()
       songArtist.Text =""
       genre.Text =""
       origin.Text =""
       artistInformation.Text =""


   结束子

   私有Sub btnclear_Click(作为对象发送,作为EventArgs发送)处理btnclear.Click
       songTitle.Clear()
       songArtist.Clear()
       songAlbum.Clear()
       yearReleased.Clear()
       datePlayed.Clear()
       timePlayed.Clear()
       genre.Clear()
       artistInformation.Clear()
   结束子


   私人子FillByToolStripButton_Click(作为对象发送,作为EventArgs发送)
      试试

      捕获为System.Exception
           System.Windows.Forms.MessageBox.Show(ex.Message)
      结束尝试

   结束子

   私有子Music_DatabaseBindingNavigatorSaveItem_Click(作为对象发送,作为EventArgs发送)处理Music_DatabaseBindingNavigatorSaveItem.Click
       Me.Validate()
       Me.Music_DatabaseBindingSource.EndEdit()
       Me.TableAdapterManager.UpdateAll(Me.Music_DatabaseDataSet)

   结束子

   私有子Form1_Load(作为对象发送,作为EventArgs发送)处理MyBase.Load
       'TODO:此代码行将数据加载到'Music_DatabaseDataSet.Song_Artist_Information'表中.您可以根据需要移动或删除它.
       Me.Song_Artist_InformationTableAdapter.Fill(Me.Music_DatabaseDataSet.Song_Artist_Information)
       'TODO:此代码行将数据加载到'Music_DatabaseDataSet.Play_Session'表中.您可以根据需要移动或删除它.
       Me.Play_SessionTableAdapter.Fill(Me.Music_DatabaseDataSet.Play_Session)
       'TODO:此代码行将数据加载到'Music_DatabaseDataSet.Music_Database'表中.您可以根据需要移动或删除它.
       Me.Music_DatabaseTableAdapter.Fill(Me.Music_DatabaseDataSet.Music_Database)


       

 

 
   结束子

   


   私有Sub btnupdate1_Click(作为对象发送,作为EventArgs发送)处理btnupdate1.Click
       Me.Validate()
       con.Open()
       ds.Tables.Add(dt)
       da = New OleDbDataAdapter(从[音乐数据库]中选择*",同上)
      昏暗的cb =新的OleDbCommandBuilder(da)
       cb.QuotePrefix ="[""
       cb.QuoteSuffix =]"
       da.Fill(dt)
       Music_DatabaseDataGridView.DataSource = dt.DefaultView
       da.Update(dt)
   结束子

   私有Sub btnupdate2_Click(发送者作为对象,e作为EventArgs)处理btnupdate2.Click
       Me.Validate()
       con.Open()
       ds.Tables.Add(dt)
       da = New OleDbDataAdapter(从[Play Session]中选择*",同上)
      昏暗的cb =新的OleDbCommandBuilder(da)
       cb.QuotePrefix ="[""
       cb.QuoteSuffix =]"
       da.Fill(dt)
       Play_SessionDataGridView.DataSource = dt.DefaultView

       da.Update(dt)


   结束子

   私有Sub btnupdate3_Click(作为对象发送,作为EventArgs发送)处理btnupdate3.Click
       Me.Validate()
       con.Open()
       ds.Tables.Add(dt)
       da = New OleDbDataAdapter(从[歌曲艺术家信息]中选择*",同上)
      昏暗的cb =新的OleDbCommandBuilder(da)
       cb.QuotePrefix ="[""
       cb.QuoteSuffix =]"
       da.Fill(dt)
       Song_Artist_InformationDataGridView.DataSource = dt.DefaultView


       da.Update(dt)
   结束子
结束班

Imports System.Data.OleDb

Public Class Form1

    Dim provider As String
    Dim datafile As String
    Dim connString As String
    Dim con As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source=G:\Music Session Database\Music Database.accdb")
    Dim ds As New DataSet

    Dim dt As New DataTable

    Dim da As New OleDbDataAdapter

    Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles btnexit.Click
        Me.Close()
    End Sub



    Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnsubmit1.Click

        Me.Music_DatabaseTableAdapter.Insert(Me.songTitle.Text, Me.songArtist.Text, Me.songAlbum.Text, Me.yearReleased.Text)
        Me.Music_DatabaseTableAdapter.Fill(Me.Music_DatabaseDataSet.Music_Database)


        con.Open()
        MsgBox("Record Added")
        con.Close()
        songTitle.Text = ""
        songArtist.Text = ""
        songAlbum.Text = ""
        yearReleased.Text = ""


    End Sub


    Private Sub btnsumbit2_Click(sender As Object, e As EventArgs) Handles btnsumbit2.Click

        Me.Play_SessionTableAdapter.Insert(Me.songTitle.Text, Me.songArtist.Text, Me.songAlbum.Text, Me.yearReleased.Text, Me.datePlayed.Text, Me.timePlayed.Text, Me.genre.Text)
        Me.Play_SessionTableAdapter.Fill(Me.Music_DatabaseDataSet.Play_Session)


        con.Open()
        MsgBox("Record Added")
        con.Close()
        songTitle.Text = ""
        songArtist.Text = ""
        songAlbum.Text = ""
        yearReleased.Text = ""
        datePlayed.Text = ""
        timePlayed.Text = ""
        genre.Text = ""



    End Sub

    Private Sub btnsubmit3_Click(sender As Object, e As EventArgs) Handles btnsubmit3.Click
      

        Me.Song_Artist_InformationTableAdapter.Insert(Me.songArtist.Text, Me.genre.Text, Me.origin.Text, Me.artistInformation.Text)
        Me.Song_Artist_InformationTableAdapter.Fill(Me.Music_DatabaseDataSet.Song_Artist_Information)

        con.Open()
        MsgBox("Record Added")
        con.Close()
        songArtist.Text = ""
        genre.Text = ""
        origin.Text = ""
        artistInformation.Text = ""


    End Sub

    Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles btnclear.Click
        songTitle.Clear()
        songArtist.Clear()
        songAlbum.Clear()
        yearReleased.Clear()
        datePlayed.Clear()
        timePlayed.Clear()
        genre.Clear()
        artistInformation.Clear()
    End Sub


    Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs)
        Try

        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub Music_DatabaseBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Music_DatabaseBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Music_DatabaseBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Music_DatabaseDataSet)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Music_DatabaseDataSet.Song_Artist_Information' table. You can move, or remove it, as needed.
        Me.Song_Artist_InformationTableAdapter.Fill(Me.Music_DatabaseDataSet.Song_Artist_Information)
        'TODO: This line of code loads data into the 'Music_DatabaseDataSet.Play_Session' table. You can move, or remove it, as needed.
        Me.Play_SessionTableAdapter.Fill(Me.Music_DatabaseDataSet.Play_Session)
        'TODO: This line of code loads data into the 'Music_DatabaseDataSet.Music_Database' table. You can move, or remove it, as needed.
        Me.Music_DatabaseTableAdapter.Fill(Me.Music_DatabaseDataSet.Music_Database)


        

 

 
    End Sub

   


    Private Sub btnupdate1_Click(sender As Object, e As EventArgs) Handles btnupdate1.Click
        Me.Validate()
        con.Open()
        ds.Tables.Add(dt)
        da = New OleDbDataAdapter("Select * from [Music Database]", con)
        Dim cb = New OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"
        da.Fill(dt)
        Music_DatabaseDataGridView.DataSource = dt.DefaultView
        da.Update(dt)
    End Sub

    Private Sub btnupdate2_Click(sender As Object, e As EventArgs) Handles btnupdate2.Click
        Me.Validate()
        con.Open()
        ds.Tables.Add(dt)
        da = New OleDbDataAdapter("Select * from [Play Session]", con)
        Dim cb = New OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"
        da.Fill(dt)
        Play_SessionDataGridView.DataSource = dt.DefaultView

        da.Update(dt)


    End Sub

    Private Sub btnupdate3_Click(sender As Object, e As EventArgs) Handles btnupdate3.Click
        Me.Validate()
        con.Open()
        ds.Tables.Add(dt)
        da = New OleDbDataAdapter("Select * from [Song Artist Information]", con)
        Dim cb = New OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"
        da.Fill(dt)
        Song_Artist_InformationDataGridView.DataSource = dt.DefaultView


        da.Update(dt)
    End Sub
End Class

Wyvern

在数据库属性下,将复制到输出目录"如下所示,从始终复制"复制到如果较新则复制",现在尝试更新

Under the database properties change Copy to output Directory as follows from Copy Always to Copy if Newer now try the update.