4

I am trying to import a large number of data from a CSV file to a SQL Sever database table. I am able to write line by line but that takes too long. What I have below fails on "FROM [C:\Temp\tblOPTExportData.csv]" during oComm.Execute. Any help would be appreciated.

On Error GoTo err_me
Dim locComm As New ADODB.Command
Dim locConnection As New ADODB.Connection
Dim locRst As New ADODB.Recordset
Dim ee As Boolean
Dim su As Boolean
Dim strSQLQuery As String
Dim shtDash As Worksheet
Dim shtData As Worksheet
Dim shtOP As Worksheet

With Application
    ee = .EnableEvents
    su = .ScreenUpdating
    If ee Then .EnableEvents = False
    If Not su Then .ScreenUpdating = True
End With

With ThisWorkbook
    Set shtDash = .Sheets("Dashboard")
    Set shtData = .Sheets("Data")
    Set shtOP = .Sheets("OP")
End With

With locConnection
    .CommandTimeout = 0
    .ConnectionString = "Provider=SQLOLEDB;Server=sql-ewhcld-1000; Database=xxxxxxxxxxxxxx; User ID=tenant-xxxxxxxxxxxxxxx; Password=yeahidontthinkso; Trusted_Connection=True; Pooling=True; MultipleActiveResultSets=False"
    .Open
End With

'    ____________________________
'   /                            \
'  |    IMS Factory Model Data    |
'   \____________________________/
'
'With statRng
'    .Value = "Factory Model Phase Data // Importing"
'    .Font.Color = 8421504
'    .Characters(Start:=29, Length:=9).Font.Color = 10192433 'Blue
'End With

With shtOP
    endRow = .Cells(.Rows.count, 2).End(xlUp).Row 'B (2)
End With
If endRow < 3 Then Err.Raise Number:=vbObjectError + 20002, Source:="exportData_Excel", Description:="No data found: 'OP' sheet, column 2 (B)."
If Not rangetoCSV("B3:K" & endRow, "tblOPTExportData", 201, , , "OP") Then Err.Raise Number:=vbObjectError + 30001, Description:="rangetoCSV, 'tblGates'"

strSQLQuery = "INSERT INTO optData (opsType, opsUID, opsDesc, opsProgram, opsFlight, opsProductAreaL1, opsAssignee, opsGenDate, opsECD, opsStatus) " & _
              "SELECT Type, UID, Description, Program, Flight, L-1 IPT, Assignee, Generated, ECD, Status FROM [C:\Temp\tblOPTExportData.csv]"

With oComm
    .ActiveConnection = locConnection
    .CommandText = strSQLQuery
    .Execute
End With
3
  • If performance is your concern, why do you need to do this in Excel? How many rows are you talking about and how fast is fast enough? Commented Jul 13, 2014 at 10:01
  • @NYCdotNet, data seems to originate in Excel, and is exported to csv file simply to do the bulk import. Commented Jul 14, 2014 at 16:50
  • 2
    @"randomdownvoter" Why the down vote? Down votes should be accompanied by an explanation, especially for someone asking their first question. Commented Jul 14, 2014 at 16:54

1 Answer 1

2

You need to use BULK INSERT rather than INSERT INTO. Try something like this:

strSQLQuery = "BULK INSERT optData " & _
              "FROM C:\Temp\tblOPTExportData.csv " & _
              "WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', " & _
              "ROWTERMINATOR = '\n', TABLOCK)"  
    With oComm
        .ActiveConnection = locConnection
        .CommandType = adCmdText
        .CommandText = strSQLQuery
        .Execute
    End With
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.