如何將Access和Excel匯入到Mysql中之三

來源:互聯網
上載者:User
access|excel|mysql 二。導庫的VB程式

  這個工程要使用一些物件程式庫,在資料庫訪問方面,決定使用ADO(ActiveX Data Objects),對於使用過ASP的人,這個名字應該非常熟悉了,實際上,它在VB中的應用也是幾乎一樣的,由於要操作Excel,還要引用一個擴充的物件程式庫。點菜單中的“工程”---“引用”,選擇“Microsoft ActiveX Data Objects 2.6 Library”和“Microsoft Excel 9.0 Object Library”,在引用列表中,可引用物件程式庫的版本或許有所不同,選擇類似的最新版本就可以了。要使用對話方塊和進度條,還要引用一些組件,點菜單中的“工程”--“組件”,選擇“Microsoft Common Dialog Control 6.0(SP3)”和“Microsoft Windows Common Controls 6.0(SP3)”。 

介面如圖一所示

圖中各數字表示的控制項分別是: 

1-3:3個均為TextBox控制項,名字分別為mdbfilename,mdbpassword,xlsfilename,mdbfilename用來指示匯入Access檔案的路徑和檔案名稱,mdbpassword用來輸入Access檔案的密碼,xlsfilename用來指示匯入Excel檔案的路徑和檔案名稱; 

4:名稱為select_mdbfile,CommandButton控制項,用來選擇要匯入的Access檔案; 

5:名稱為importMDB,CommandButton控制項,選擇好檔案後,用該按鈕匯入MDB檔案 

6:名稱為CommonDialog1,CommonDialog控制項 

7:名稱為select_xlsfile,CommandButton控制項,用來選擇要匯入的Excel檔案; 

8:名稱為importXLS,CommandButton控制項,選擇好檔案後,用該按鈕匯入XLS檔案; 

9:名稱為CommonDialog2,CommonDialog控制項 

10:名稱為prgBar1,進度條(ProgressBar),當要匯入很多記錄時,使用進度條可以指示當前的匯入進度 

11:名稱為StatusBar1,狀態條(StatusBar),指示匯入的進展狀態,包括“正在匯入”,“匯入完成”等。 

要將Style設定為“1-sbrSimple” 


在程式的頭部作以下聲明 

Option Explicit 

Dim Cn As New ADODB.Connection 

Dim Cnmysql As New ADODB.Connection 

Dim Rs As New ADODB.Recordset 

Dim Rsmysql As New ADODB.Recordset 


Function replace_str(srcstr As String) 

srcstr = Replace(srcstr, "\", "\\") 

replace_str = Replace(srcstr, "'", "\'") 

End Function 



雙擊form,在裝載form時輸入以下的代碼 

Private Sub Form_Load() 

'將進度條設定為不可見 

prgBar1.Visible = False 

'串連mysql資料庫,pwd為資料庫的密碼,uid為使用者名稱,dsn為資料來源的名稱 

Cnmysql.ConnectionString = "uid=root;pwd=;dsn=build" 

Cnmysql.Open 

End Sub 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.