Pandy_PB
Pandy_PB
采纳率50%
2021-03-01 16:01

VB.NET Access数据库更新问题,请帮忙指正!

10
已结题
Public Class Form1
    Dim SqlConn As OleDbConnection
    Dim SqlUse1 As OleDbDataAdapter
    Dim Dt1 As DataTable

'绑定数据库,显示DGV中
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SqlConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Schedule Tool\Tool\Projects_Schedule_Database.accdb")
        SqlUse1 = New OleDbDataAdapter("SELECT * FROM China_Projects", SqlConn)       
        Dt1 = New DataTable
        SqlUse1.Fill(Dt1)
        DataGridView1.DataSource = Dt1.DefaultView

  End Sub

'更新数据库
  Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
        Dim OleCb1 As New OleDbCommandBuilder(SqlUse1)
        OleCb1.QuotePrefix = "["
        OleCb1.QuoteSuffix = "]"
        SqlUse1.UpdateCommand = OleCb1.GetUpdateCommand()
        SqlUse1.Update(Dt1) 
        Dt1.AcceptChanges() '此处是否可省略?
        MsgBox("Done")
    End Sub
End class


''问题,手动更改DGV中的单元格后,点击Button数据库可以更新。但通过控件如Textbox写入的字符串(这里指控件写入DGV单元格中)无法更新数据库字段.

''描述1,此数据库中有三张table
''描述2,确定每张表格都有主键
''描述3,若是在DGV中新增加的行(行内有内容),数据库可以保存更新
''描述4,多次保存后(n>4),提示(“违反并发:Updatecommand 影响了预期...”)

''请帮忙指正,谢谢

  • 点赞
  • 收藏
  • 复制链接分享

3条回答