0

我正在尝试从 VB.net 插入 Microsoft Access 数据库。我可以从数据库中读取(可以在网格视图中查看)但无法插入数据库。我不知道问题出在哪里。这就是我正在做的事情:

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
Public ctr As Integer
Dim bm As BindingManagerBase
Dim dr As DataRow, dt As DataTable
Dim flag As Integer, tid As Integer, tname As String
Dim totalrow As Integer, currentrow As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'ShowroomDataSet.customer' table. You can move, or remove it, as needed.
    bm = Me.BindingContext(ShowroomDataSet, "customer")
    bm.Position = 0
    Me.CustomerTableAdapter.Fill(Me.ShowroomDataSet.customer)

 End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x As Integer
    Dim y As String
    bm.Position = bm.Count - 1
    x = ShowroomDataSet.customer(bm.Position).cid
    y = ShowroomDataSet.customer(bm.Position).cname
    TextBox1.Text = x
    TextBox2.Text = y

  End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'save
    'enable all buttons except save
    If flag = 1 Then
        dt = ShowroomDataSet.Tables("customer")
        dr = dt.NewRow()
        dr!cid = Val(TextBox1.Text)
        dr!cname = TextBox2.Text
        dt.Rows.Add(dr)

    End If
    If flag = 2 Then
        dr.Delete()
    End If
    If flag = 3 Then
        dt = ShowroomDataSet.Tables("customer")
        dr = dt.Rows.Find(tid)
        dr.BeginEdit()
        dr!cid = Val(TextBox1.Text)
        dr!name = TextBox2.Text
        dr.EndEdit()
    End If
    'Me.CustomerTableAdapter.Update(Me.ShowroomDataSet.customer)
    'Me.CustomerTableAdapter.Fill(Me.ShowroomDataSet.customer)
    flag = 0
    Button1.Enabled = True
    Button2.Enabled = False
    Button3.Enabled = True
    Button4.Enabled = True

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    'add
    'disable all buttons except save
    Dim len As Integer
    TextBox1.Text = " "
    TextBox2.Text = " "
    flag = 1
    TextBox1.Focus()
    Button1.Enabled = False
    Button2.Enabled = True
    Button3.Enabled = False
    Button4.Enabled = False
    dt = ShowroomDataSet.Tables("customer")
    len = dt.Rows.Count - 1
    dr = dt.Rows(len)
  End Sub
  End Class

没有错误或故障。请帮忙。

4

1 回答 1

1

如果您只是想将数据插入到 access 数据库中,请使用数据库表适配器插入方法,然后使用表适配器的填充方法来填充数据集中的表。请参阅以下示例

me.tableadapter.insert(me.1stFieldTextbox.text, me.2ndFieldTextBox.Text....) me.tableadapter.fill(me.Dataset.Table)

如果您收到错误,可能是因为您的数据类型不匹配,您尝试将数据插入自动增量字段或具有计算数据类型的字段。

于 2012-11-28T09:52:21.813 回答