萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

VB6 使用 DAO 建立資料庫並從文字檔匯入資料

本文示範在 VB6 中不依賴 Access,直接以程式碼建立 Jet 資料庫(.mdb),從 ASCII 文字檔讀取資料寫入資料表,並以 MSFlexGrid 顯示結果。原為 VB3 範例,已更新為 VB6 + DAO 3.51 相容版本。

專案準備

  1. 專案 → 參考 → 勾選 Microsoft DAO 3.51 Library
  2. 表單放置三個 CommandButton、兩個 MSFlexGrid
  3. 設定控制項屬性(Caption、Cols、Rows、ColWidth 等)

宣告區程式碼

Dim nums(30) As Long
Dim names(30) As String * 20
Dim addresses(30) As String * 25
Dim ss_nums(30) As String * 12
Const DB_LONG = 4
Const DB_TEXT = 10
Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"

Form_Load:初始化 Grid 標題

Private Sub Form_Load()
    Me.Show
    ' Grid1 顯示文字檔資料
    Grid1.ColWidth(1) = 1000: Grid1.Col = 1: Grid1.Row = 0: Grid1.Text = "EmpID"
    Grid1.ColWidth(2) = 2000: Grid1.Col = 2: Grid1.Row = 0: Grid1.Text = "EmpName"
    Grid1.ColWidth(3) = 3000: Grid1.Col = 3: Grid1.Row = 0: Grid1.Text = "EmpAddr"
    Grid1.ColWidth(4) = 2000: Grid1.Col = 4: Grid1.Row = 0: Grid1.Text = "EmpSSN"
    ' Grid2 顯示資料庫資料
    Grid2.ColWidth(1) = 1000: Grid2.Col = 1: Grid2.Row = 0: Grid2.Text = "EmployeeID"
    Grid2.ColWidth(2) = 2000: Grid2.Col = 2: Grid2.Row = 0: Grid2.Text = "EmployeeName"
    Grid2.ColWidth(3) = 3000: Grid2.Col = 3: Grid2.Row = 0: Grid2.Text = "EmployeeAddr"
    Grid2.ColWidth(4) = 2000: Grid2.Col = 4: Grid2.Row = 0: Grid2.Text = "EmployeeSSN"
End Sub

Command1_Click:建立測試文字檔並顯示

Private Sub Command1_Click()
    ' 產生 30 筆測試資料
    For i = 1 To 30
        nums(i) = i
        names(i) = "JohnDoe#" & Str$(i)
        addresses(i) = Str$(i) & "MockingBirdLane"
        If i < 9 Then
            ss_nums(i) = Trim$(Str$(i) & Trim$(Str$(i)) & Trim$(Str$(i)) & "-" & _
                           Trim$(Str$(i + 1)) & Trim$(Str$(i + 1)) & "-" & _
                           Trim$(Str$(i)) & Trim$(Str$(i)) & Trim$(Str$(i)))
        Else
            ss_nums(i) = Trim$(Trim$(Str$(999)) & "-" & Trim$(Str$(88)) & "-" & Trim$(Str$(7777)))
        End If
    Next i
    ' 寫入文字檔
    Open "Testdata.DAT" For Output As #1
    For j = 1 To 30
        Print #1, nums(j)
        Print #1, names(j)
        Print #1, addresses(j)
        Print #1, ss_nums(j)
    Next j
    Close #1
    ' 顯示於 Grid1
    For i = 1 To 30
        Grid1.Col = 1: Grid1.Row = i: Grid1.Text = nums(i)
        Grid1.Col = 2: Grid1.Row = i: Grid1.Text = names(i)
        Grid1.Col = 3: Grid1.Row = i: Grid1.Text = addresses(i)
        Grid1.Col = 4: Grid1.Row = i: Grid1.Text = ss_nums(i)
    Next i
End Sub

Command2_Click:建立資料庫並匯入資料

Private Sub Command2_Click()
    Dim newdb As Database
    Dim newtb As Recordset
    Dim newtd As New TableDef
    Dim newidx As New Index
    Dim field1 As New Field, field2 As New Field, field3 As New Field, field4 As New Field

    Screen.MousePointer = 11
    Set newdb = CreateDatabase("NEWDB.MDB", DB_LANG_GENERAL)
    newtd.Name = "Emp_Table"
    ' 定義欄位
    field1.Name = "Emp_ID": field1.Type = DB_LONG: newtd.Fields.Append field1
    field2.Name = "Emp_Name": field2.Type = DB_TEXT: field2.Size = 20: newtd.Fields.Append field2
    field3.Name = "Emp_Addr": field3.Type = DB_TEXT: field3.Size = 25: newtd.Fields.Append field3
    field4.Name = "Emp_SSN": field4.Type = DB_TEXT: field4.Size = 12: newtd.Fields.Append field4
    ' 主鍵索引
    newidx.Name = "Emp_ID_IDX": newidx.Fields = "Emp_ID": newidx.Primary = True
    newtd.Indexes.Append newidx
    newdb.TableDefs.Append newtd
    Set newtb = newdb.OpenRecordset("Emp_Table")
    ' 讀取文字檔寫入資料庫
    Open "Testdata.dat" For Input As #1
    BeginTrans
    Do While Not EOF(1)
        newtb.AddNew
        Line Input #1, tmp1$: newtb("Emp_ID") = Trim$(tmp1$)
        Line Input #1, tmp2$: newtb("Emp_Name") = Trim$(tmp2$)
        Line Input #1, tmp3$: newtb("Emp_Addr") = Trim$(tmp3$)
        Line Input #1, tmp4$: newtb("Emp_SSN") = Trim$(tmp4$)
        newtb.Update
    Loop
    CommitTrans
    Close #1
    newtb.Close: newdb.Close
    Screen.MousePointer = 0
End Sub

Command3_Click:讀取資料庫顯示於 Grid2

Private Sub Command3_Click()
    Dim db As Database, t As Recordset, counter As Long
    Set db = OpenDatabase("NEWDB.MDB")
    Set t = db.OpenRecordset("Emp_Table")
    counter = 1
    Do Until t.EOF
        Grid2.Col = 1: Grid2.Row = counter: Grid2.Text = t(0)
        Grid2.Col = 2: Grid2.Row = counter: Grid2.Text = t(1)
        Grid2.Col = 3: Grid2.Row = counter: Grid2.Text = t(2)
        Grid2.Col = 4: Grid2.Row = counter: Grid2.Text = t(3)
        counter = counter + 1
        t.MoveNext
    Loop
    t.Close: db.Close
End Sub

關鍵技術點

  • CreateDatabase 建立 .mdb,TableDef/Field/Index 定義結構
  • OpenRecordset 取得可更新的 Recordset
  • BeginTrans/CommitTrans 交易控制,確保批次寫入一致性
  • 文字檔用 Line Input 逐行讀取,Trim$ 去除空白

此範例展示 VB6 以 DAO 直接操作 Jet Engine 的完整流程,適合輕量級資料匯入需求。

copyright © 萬盛學電腦網 all rights reserved