2
많은 수의 데이터를 CSV 파일에서 SQL Sever 데이터베이스 테이블로 가져 오려고합니다. 줄 단위로 작성할 수는 있지만 너무 오래 걸립니다. oComm.Execute 중에 "FROM [C : \ Temp \ tblOPTExportData.csv]"에서 아래의 오류가 발생합니다. 어떤 도움을 주시면 감사하겠습니다.Excel VBA를 사용하여 CSV에서 SQL Server로 대량 가져 오기 ADODB
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
성능이 걱정되면 Excel에서 왜이 작업을 수행해야합니까? 얼마나 많은 행에 대해 이야기하고 있으며 얼마나 빨리 빠릅니까? – NYCdotNet
@ NYCdotNet은 데이터가 Excel에서 비롯된 것으로 보이며 대량 가져 오기를 수행하기 위해 CSV 파일로 내보내집니다. –
@ "randomdownvoter"왜 투표가 끝났습니까? 아래 표는 특히 첫 번째 질문을하는 사람에게 설명이 첨부되어야합니다. –