VB.NET:将数据插入连接到MS Access数据库的数据网格视图

时间:2019-12-22 17:15:22

标签: database vb.net ms-access datagridview visual-studio-2019

我正在创建一个系统,用户可以在其中选择所需的书。在开发系统时,创建管理模块对系统同样重要。

这是我自己的问题,我想从已经连接到MS Access数据库的DataGridView插入数据。它不会给我错误,但是当我运行它并对其进行测试时,似乎没有在程序上写入处理内容。错误是

  

System.InvalidOperationException:'当控件与数据绑定时,行不能以编程方式添加到DataGridView的行集合中。'

我真正想要的是可以使用连接到的MS Access同时更新DataGridView。

Imports System.Data.OleDb

Public Class Register
    Dim provider As String
    Dim datafile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Text.Equals("TVL12") Then
            TVL12.Show()
        End If
    End Sub

    Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click
        Admin.table.Rows.Add(booknameReg.Text, authorReg.Text, publisherReg.Text, subjectcodeReg.Text, priceReg.Text, discountamountReg.Text)
        'TVL12.BookListTVL.Rows.Add(booknameReg.Text, authorReg.Text, publisherReg.Text, subjectcodeReg.Text, priceReg.Text, discountamountReg.Text)'
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        datafile = "C:\Users\acjfc\OneDrive\Desktop\thesis\DATABASE\BookLists.accdb"
        connString = provider & datafile
        myConnection.ConnectionString = connString
        myConnection.Open()
        Dim str As String
        str = "Insert into TVL12([Book Name],[Author],[Publisher],[Subject Code],[Price],[DiscountAmount]) Values (?,?,?,?,?,?)"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        cmd.Parameters.Add(New OleDbParameter("Book Name", CType(booknameReg.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("Author", CType(authorReg.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("Publisher", CType(publisherReg.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("Subject Code", CType(subjectcodeReg.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("Price", CType(priceReg.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("DiscountAmount", CType(discountamountReg.Text, String)))
        Try
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            myConnection.Close()
            booknameReg.Clear()
            authorReg.Clear()
            publisherReg.Clear()
            subjectcodeReg.Clear()
            priceReg.Clear()
            discountamountReg.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Admin.MainData.DataSource = Admin.table
    End Sub
End Class

如您所见,代码“ TVL12.BookListTVL.Rows.Add(booknameReg.Text,authorReg.Text,publisherReg.Text,subjectcodeReg.Text,priceReg.Text,discountamountReg.Text)”已变成注释如果我删除“”东西,它将无法正常工作。尽管我用另一个未真正连接到数据库的方法完成了此操作。有人可以帮我解决这里的问题吗?

2 个答案:

答案 0 :(得分:0)

根据您的描述,您想在向数据库添加数据时更新datagridview。

您可以参考我编写的修改后的代码。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim newtable = New DataTable()
    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    datafile = "C:\Users\testuser\Documents\BookLists.accdb"
    connString = provider & datafile
    myConnection.ConnectionString = connString
    myConnection.Open()
    Dim str As String
    'str = "Insert into TVL12([Name],[Age],[StuId]) Values (?,?,?)"
    'Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    'cmd.Parameters.Add(New OleDbParameter("Name", CType("test4", String)))
    'cmd.Parameters.Add(New OleDbParameter("Age", CType(26, Int32)))
    'cmd.Parameters.Add(New OleDbParameter("StuId", CType(1004, Int32)))

    Try
        'cmd.ExecuteNonQuery()
        str = "Select * from TVL12"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        Dim da As OleDbDataAdapter = New OleDbDataAdapter()
        da.SelectCommand = cmd
        da.Fill(newtable)
        cmd.Dispose()
        myConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    DataGridView1.DataSource = newtable
End Sub

答案 1 :(得分:0)

您有一个添加按钮,可在数据库表中插入新记录。尝试使您的sub和function只做一件事。您可以在插入后以及在Form.Load中调用DisplayData

我质疑表中某些字段的数据类型。如果主题代码是数字,则它应该是整数。价格和折扣金额应为小数。

我不得不猜测您的参数是什么类型。检查数据库中的真实类型。 .Add方法的最后一个参数是字段的大小。不必在.ToString属性上调用.Text。已经是一个字符串。

使用...结束使用块关闭并处置数据库对象,即使出现错误也是如此。

Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click
    Try
        Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\acjfc\OneDrive\Desktop\thesis\DATABASE\BookLists.accdb"),
            cmd As New OleDbCommand("Insert into TVL12 ([Book Name],[Author],[Publisher],[Subject Code],[Price],[DiscountAmount]) Values (?,?,?,?,?,?)", myConnection)
            cmd.Parameters.Add("BookName", OleDbType.VarChar, 200).Value = booknameReg.Text
            cmd.Parameters.Add("Author", OleDbType.VarChar, 200).Value = authorReg.Text
            cmd.Parameters.Add("Publisher", OleDbType.VarChar, 200).Value = publisherReg.Text
            cmd.Parameters.Add("SubjectCode", OleDbType.VarChar, 200).Value = subjectcodeReg.Text
            cmd.Parameters.Add("Price", OleDbType.VarChar, 100).Value = priceReg.Text
            cmd.Parameters.Add("DiscountAmount", OleDbType.VarChar, 50).Value = discountamountReg.Text
            myConnection.Open()
            cmd.ExecuteNonQuery()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    ClearTextBoxes()
    DisplayData()
End Sub

Private Sub ClearTextBoxes()
    booknameReg.Clear()
    authorReg.Clear()
    publisherReg.Clear()
    subjectcodeReg.Clear()
    priceReg.Clear()
    discountamountReg.Clear()
End Sub

Private Sub DisplayData()
    Dim dt As New DataTable
    Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\acjfc\OneDrive\Desktop\thesis\DATABASE\BookLists.accdb"),
            cmd As New OleDbCommand("Select * From TVL12", myConnection)
        myConnection.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    DataGridView1.DataSource = dt
End Sub