2012-11-28 1 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가 프로젝트 폴더에서 바인드 폴더로 액세스 파일을 복사한다는 것을 알고 있습니다. 즉, 각각의 실행마다 새로운 복사본을 작성한다는 의미입니까? 동작은 파일의 속성 창에서 변경할 수 있습니다 -> 출력 디렉토리에 복사 : NEVER – igrimpe

+0

@ 오류가 없습니다. 추가 된 행은 Access에 반영되어야합니다. 그렇지 않습니다. 어떻게해야합니까? –

+1

올바른 파일을 확인하셨습니까? 앞에서 설명한대로 : 기본적으로 (일반적으로 비어있는) Access db 파일은 프로젝트 디렉토리에서 실행 파일이있는 하위 디렉토리 (예 :/bin/debug)로 복사됩니다. 그래서 매번 실행하면 변경된 내용이 모두 덮어 쓰여 지므로 마지막 실행의 모든 ​​변경 사항이 손실됩니다. – igrimpe

답변

1

액세스 데이터베이스에 데이터를 삽입하려는 경우 데이터베이스 어댑터 삽입 메서드를 사용하고 테이블 어댑터의 fill 메서드를 사용하여 데이터 집합의 테이블을 채 웁니다. 예를

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

당신이 얻을 경우 다음을 참조하십시오 오류가 발생했습니다. 데이터 유형이 일치하지 않아 자동 증가 필드 또는 계산 된 데이터 유형이있는 필드에 데이터를 삽입하려고했기 때문일 수 있습니다.

+0

하지만이 코드를 사용하여 잘 작동해야한다고 생각합니다. 문제는 여기에 무엇입니까? –

+0

안녕하세요 감사합니다 :) 나는 당신의 의견과 igrimpe의 감사를 생각할 때 효과적이었습니다. –

관련 문제