2014-01-20 43 views
1

這是我的代碼,但它將大約一個小時將所有1700萬行導出到mdb中。我無法爲此使用mysql或sql server。我必須在訪問數據庫中執行此操作,並快速將此過程在一週內運行一次。 Plz建議可用於此任務的最快方法使用ADO將大型csv文件導入到mdb時的性能問題

Sub insertDataIntoMDB() 
Dim Dbfilepath As String 
Set cnn = CreateObject("ADODB.Connection") 
Set rs = CreateObject("ADODB.Recordset") 
Set rst = CreateObject("ADODB.Recordset") 

     Dim arrData() As String 
     Dim s As String 
     Dim i As Integer 


     Dbfilepath = ThisWorkbook.Path & "\DB\Interface.accdb" 
     cnn.Open "Provider= Microsoft.ACE.OLEDB.12.0;" & " Data Source=" & Dbfilepath & ";" & "Persist Security Info =False;" 

     q1 = "DELETE * FROM MYTABLE" 
     Set rs = cnn.Execute(q1) 

     'q1 = "ALTER TABLE MyTable ALTER COLUMN ID autonumber(1,1)" 
     'Set rs = cnn.Execute(q1) 

     p = UserForm1.csvFolder & "\" & sItem & ".csv" 

     Open p For Input As #1 
     Do While Not EOF(1) 

      Close #1 


      Line Input #1, s 
      arrData = Split(s, ",") 
      q1 = "INSERT INTO MyTable(F1,F2,F3) values(" & arrData(0) & "," & arrData(1) & "," & arrData(2) & ")" 
      Set rst = cnn.Execute(q1) 
     Loop 
     Close #1 
     rs.Close 
     rs`enter code here`t.Close 
     Set rst = Nothing 
     cnn.Close 
     Set rs = Nothing 
     Set cnn = Nothing 


End Sub 
+0

請詳細介紹一下所有使用表格(MYTABLE)和CSV文件的情況,它們的外觀如何? (結構,數據)等。 –

+0

你爲什麼希望使用MS Access?例如。 DoCmd.TransferText? – user1917229

回答

0

就在這裏,您有一個巨大的放緩和潛在的數據損壞。

q1 = "INSERT INTO MyTable(F1,F2,F3) values(" & arrData(0) & "," & arrData(1) & "," & arrData(2) & ")" 
Set rst = cnn.Execute(q1) 

字符串串聯速度很慢,特別是在VBA中。所以,只要你寫"something" & "something"並把它放在一個循環中,你就要求緩慢的表現。

此外,使用DAO的訪問速度通常比使用ADO的速度快。

Read this answer
And maybe this question and its answers

如果你堅持使用ADO,你可能想打開記錄使用SELECT語句,然後將數據追加到該記錄,然後到的UpdateBatch調用。

You can read more discussion here

祝你好運!