2012-11-28 71 views
0

我想從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 

沒有錯誤或故障。 請幫忙。

+0

錯誤信息?你知道VS通常會將Access文件從項目文件夾複製到綁定文件夾,這意味着您在每次運行時都要使用全新副本?行爲可以在文件的屬性窗口中更改 - >複製到輸出目錄:NEVER – igrimpe

+0

@igrimpe沒有錯誤。添加的行應該反映在Access中,但它不是。我該怎麼辦? –

+1

你檢查正確的文件?正如我已經說過的那樣:BY DEFAULT(通常爲空)Access數據庫文件從項目目錄複製到可執行文件所在的子目錄(例如/ bin/debug)。因此,在每次運行時,都會覆蓋所有更改,並且您上次運行的任何更改都會丟失。 – igrimpe

回答

1

如果您只是想要將數據插入到訪問數據庫中,請使用數據庫表適配器插入方法,然後使用表適配器的填充方法填充數據集中的表。見下面的例子

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

如果你得到一個錯誤,可能是因爲您的數據類型不匹配,您嘗試將數據插入自動增量字段或具有計算數據類型的數據。

+0

但我認爲它應該使用此代碼正常工作。這裏的問題是什麼? –

+0

嘿thankx :)它的工作,當我認爲你和igrimpe感謝評論。 –