系列教學第二部分:使用 VB6 + ADO 2.6 + Excel 9.0 Object Library 開發視窗程式,批次匯入 Access MDB 與 Excel XLS 檔案至 MySQL。
| 編號 | 控制項 | 名稱 | 用途 |
|---|---|---|---|
| 1-3 | TextBox | mdbfilename, mdbpassword, xlsfilename | 檔案路徑、MDB 密碼、XLS 路徑 |
| 4 | CommandButton | select_mdbfile | 選擇 MDB 檔案 |
| 5 | CommandButton | importMDB | 執行 MDB 匯入 |
| 6 | CommonDialog | CommonDialog1 | MDB 檔案對話框 |
| 7 | CommandButton | select_xlsfile | 選擇 XLS 檔案 |
| 8 | CommandButton | importXLS | 執行 XLS 匯入 |
| 9 | CommonDialog | CommonDialog2 | XLS 檔案對話框 |
| 10 | ProgressBar | prgBar1 | 匯入進度顯示 |
| 11 | StatusBar | StatusBar1 | 狀態訊息 (Style=1-sbrSimple) |
Option Explicit
Dim Cn As New ADODB.Connection ' Access/Excel 連線
Dim Cnmysql As New ADODB.Connection ' MySQL 連線
Dim Rs As New ADODB.Recordset ' 來源 Recordset
Dim Rsmysql As New ADODB.Recordset ' 目標 Recordset (可選)
' 字串清理:移除雙引號、單引號轉義
Function replace_str(srcstr As String) As String
srcstr = Replace(srcstr, """", "")
replace_str = Replace(srcstr, "'", "''")
End Function
Private Sub Form_Load()
prgBar1.Visible = False
' MySQL 連線 (DSN-less 或 DSN)
Cnmysql.ConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=build;Uid=root;Pwd=yourpwd;Option=3;"
Cnmysql.Open
End Sub
' 選擇 MDB 檔案
Private Sub select_mdbfile_Click()
CommonDialog1.Filter = "Access Files (*.mdb)|*.mdb"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
mdbfilename.Text = CommonDialog1.FileName
End If
End Sub
' 匯入 MDB 主邏輯
Private Sub importMDB_Click()
If mdbfilename.Text = "" Then MsgBox "請先選擇 MDB 檔案": Exit Sub
' 開啟 Access 連線
Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbfilename.Text & ";"
If mdbpassword.Text <> "" Then Cn.ConnectionString = Cn.ConnectionString & "Jet OLEDB:Database Password=" & mdbpassword.Text
Cn.Open
' 讀取來源資料 (假設來源表名為 loupan)
Set Rs = Cn.Execute("SELECT * FROM loupan")
If Rs.EOF And Rs.BOF Then MsgBox "來源無資料": Exit Sub
' 進度條初始化
Rs.MoveLast: Rs.MoveFirst
prgBar1.Min = 0: prgBar1.Max = Rs.RecordCount: prgBar1.Value = 0: prgBar1.Visible = True
StatusBar1.Panels(1).Text = "正在匯入 MDB..."
' 批次寫入 MySQL (使用 ADO Command 參數化)
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = Cnmysql
cmd.CommandText = "INSERT INTO loupan (loupantype, inputdate, at_whatsection, buildname, buildaddress, room, hall, areasize, exp_sellprice, ownername, contactway, memo, updatestatus, goodloupan) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
cmd.CommandType = adCmdText
cmd.Prepared = True
Do Until Rs.EOF
' 參數繫結 (須對應順序)
cmd.Parameters(0).Value = replace_str(Rs("loupantype") & "")
cmd.Parameters(1).Value = Rs("inputdate")
cmd.Parameters(2).Value = replace_str(Rs("at_whatsection") & "")
cmd.Parameters(3).Value = replace_str(Rs("buildname") & "")
cmd.Parameters(4).Value = replace_str(Rs("buildaddress") & "")
cmd.Parameters(5).Value = Rs("room")
cmd.Parameters(6).Value = Rs("hall")
cmd.Parameters(7).Value = Rs("areasize")
cmd.Parameters(8).Value = Rs("exp_sellprice")
cmd.Parameters(9).Value = replace_str(Rs("ownername") & "")
cmd.Parameters(10).Value = replace_str(Rs("contactway") & "")
cmd.Parameters(11).Value = replace_str(Rs("memo") & "")
cmd.Parameters(12).Value = Rs("updatestatus")
cmd.Parameters(13).Value = Rs("goodloupan")
cmd.Execute
Rs.MoveNext
prgBar1.Value = prgBar1.Value + 1
DoEvents
Loop
prgBar1.Visible = False
StatusBar1.Panels(1).Text = "MDB 匯入完成,共 " & Rs.RecordCount & " 筆"
Rs.Close: Cn.Close
End Sub
' 匯入 XLS 邏輯 (類似,使用 Excel Object 開啟工作表讀取)
' Private Sub importXLS_Click() ... 略 ...
cmd.Prepared = True + ? 佔位符,防注入、提升批次效能。Rs.MoveLast 取得總筆數 (需支援書籤的游標),迴圈中更新 Value。On Error GoTo ErrHandler、交易控制 (BeginTrans/CommitTrans/RollbackTrans)。Set xlApp = New Excel.Application → xlApp.Workbooks.Open → 迴圈 Cells(row, col) 讀取 → 同樣參數化寫入 MySQL。