使用VB.net,Microsoft Access 2003在数据库中的简单插入失败

时间:2012-11-28 07:10:04

标签: database vb.net ms-access database-update

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

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

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

1 个答案:

答案 0 :(得分:1)

如果您只是想将数据插入访问数据库,请使用数据库表适配器insert方法,然后使用表适配器的fill方法填充数据集中的表。见以下示例

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

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