|
本帖最后由 kippon 于 2013-5-11 20:37 编辑
下面只对一个表操作,插入20000条数据后,大概70秒左右。
用DB将数据删除后,重新执行, 运行到Set Rst = ConnectRst(strSQL, "Postgre")就处于忙的状态。
Call FunUpSpeedStart
Application.DisplayAlerts = False
' ThisWorkbook.Saved
ThisWorkbook.ChangeFileAccess xlReadOnly
For Each tablecell In Range(Replace(importRefEditValue, "$", ""))
strTable = tablecell.Value
Cells(tablecell.Row + 1, tablecell.Column).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
arr = Selection
importProgressBar.Min = 1
importProgressBar.Max = UBound(arr)
For iRow = 1 To UBound(arr)
strKeySQL = ""
insertValues = ""
For iCol = 1 To UBound(arr, 2)
' Get data
insertValues = insertValues & "'" & arr(iRow + 1, iCol) & "',"
Next
' Insert into DB
strSQL = ""
strSQL = "INSERT INTO " & strTable & " VALUES(" & Left(insertValues, Len(insertValues) - 1) & ")"
Set Rst = ConnectRst(strSQL, "Postgre")
'Disconnect from DB
If Rst.State = 1 Then
Rst.Close
Set Rst = Nothing
End If
Cnn.Close
Set Cnn = Nothing
importProgressBar.Value = iRow
Next
Next
Call FunUpSpeedEnd
ThisWorkbook.ChangeFileAccess xlReadWrite
Application.DisplayAlerts = True
Function ConnectRst(Sql As String, sqlType As String) As ADODB.Recordset
Dim Cnn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
On Error Resume Next
' On Error GoTo l1
Select Case sqlType
Case "Excel"
Cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1';data source=" & g_excel_file
Case "Postgre"
Cnn.Open "DRIVER={" & g_db_driver & "};SERVER=" & g_db_server & ";UID=" & g_db_uid & ";PWD=" & g_db_pwd & ";DATABASE=" & g_db_name & ";Connect Timeout=720"
End Select
Rst.Open Sql, Cnn, adOpenKeyset
Set ConnectRst = Rst
End Function
谢谢指导。
|
|