我想從VB.net插入到Microsoft Access數據庫。 我可以從數據庫中讀取(可以在網格視圖中看到),但無法插入數據庫。我不知道問題出在哪裏。 這就是我在做什麼:Microsoft Access 2003簡單插入數據庫失敗,使用VB.net
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
沒有錯誤或故障。 請幫忙。
錯誤信息?你知道VS通常會將Access文件從項目文件夾複製到綁定文件夾,這意味着您在每次運行時都要使用全新副本?行爲可以在文件的屬性窗口中更改 - >複製到輸出目錄:NEVER – igrimpe
@igrimpe沒有錯誤。添加的行應該反映在Access中,但它不是。我該怎麼辦? –
你檢查正確的文件?正如我已經說過的那樣:BY DEFAULT(通常爲空)Access數據庫文件從項目目錄複製到可執行文件所在的子目錄(例如/ bin/debug)。因此,在每次運行時,都會覆蓋所有更改,並且您上次運行的任何更改都會丟失。 – igrimpe