将数据从DataGridView插入MS Access [英] Insert Data from DataGridView into MS Access

查看:71
本文介绍了将数据从DataGridView插入MS Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我快到了,但是我仍然需要帮助,将数据从datagridview插入到ms Access数据库中.我一直在点击信息按钮,但是访问文件本身没有更新.有人可以帮忙吗?您需要解决的代码将 大胆.

I'm almost there, but I still need help inserting the data from datagridview to ms access database. I keep hitting the button to information but there's no update on the access file itself. Can anyone help with this please? The code you need to address will be in bold.

导入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(发送者为对象,e作为EventArgs)处理btnupdate1.Click
      昏暗的歌曲标题为字符串
       Dim songArtist As String
      昏暗的歌曲专辑作为字符串
      昏暗的年份以字符串形式发布


       Dim str As String
       str ="INSERT INTO音乐数据库([歌曲名称],[歌曲艺术家],[歌曲专辑],[发布年份])值(@ Song_Title,@ Song_Artist,@ Song_Album,@ Year_Released)"
       Dim cmd As OleDbCommand = New OleDbCommand(str,con)
       cmd.Parameters.AddWithValue("@ Song_Title",songTitle)
       cmd.Parameters.AddWithValue("@ Song_Artist",songArtist)
       cmd.Parameters.AddWithValue("@ Song_Album",songAlbum)
       cmd.Parameters.AddWithValue("@ Year_Released",yearReleased)


       Me.Validate()
       con.Open()
       ds.Tables.Add(dt)
       con.Close()
       da = New OleDbDataAdapter(从[音乐数据库]中选择*",同上)
      昏暗的cb =新的OleDbCommandBuilder(da)
       cb.QuotePrefix ='"["
       cb.QuoteSuffix =]"
       da.Fill(dt)
       Music_DatabaseDataGridView.DataSource = dt.DefaultView
       da.Update(dt)
结束

私有Sub btnupdate2_Click(作为对象发送,作为EventArgs发送)处理btnupdate2.单击
       Me.Validate()
       con.Open()
       ds.Tables.Add(dt)
       con.Close()
       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.单击
       Me.Validate()
       con.Open()
       ds.Tables.Add(dt)
       con.Close()
       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
        Dim songTitle As String
        Dim songArtist As String
        Dim songAlbum As String
        Dim yearReleased As String


        Dim str As String
        str = "INSERT INTO Music Database([Song Title], [Song Artist], [Song Album], [Year Released]) Values (@Song_Title, @Song_Artist, @Song_Album, @Year_Released)"
        Dim cmd As OleDbCommand = New OleDbCommand(str, con)
        cmd.Parameters.AddWithValue("@Song_Title", songTitle)
        cmd.Parameters.AddWithValue("@Song_Artist", songArtist)
        cmd.Parameters.AddWithValue("@Song_Album", songAlbum)
        cmd.Parameters.AddWithValue("@Year_Released", yearReleased)


        Me.Validate()
        con.Open()
        ds.Tables.Add(dt)
        con.Close()
        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)
        con.Close()
        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)
        con.Close()
        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

飞龙

推荐答案

做起来很简单,却很难. 

You do it very difficult while it is so easy. 

在我们的网站上查看此示例.

Look at this sample on our website. 

http://www.vb-tips.com/CommandBuilderDataGridview.ASPX


请注意,数据适配器更新适用于SQL插入,更新和删除


Be aware that the dataadapter update is for SQL inserts, updates and deletes


这篇关于将数据从DataGridView插入MS Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆