I have a code that loads data from Excel into SQLServer table. It works fine, but I need to know if there's a best way to do it, I mean do the process faster. This is because in Excel file I usually have around 7000 rows and the time loading is too long (of course I'm loading row by row).
Here's the code to your analyze.
Dim varExcel As Object = CreateObject("Excel.Application")
Dim varBook As Object = varExcel.Workbooks.Open(varFileName, True, True, , "")
Dim varSheet As Object = varExcel.Worksheets(1)
Dim varArray As Object = varSheet.Range("A1:A65000").Value
varCount = 1
For vari As Integer = 2 To varArray.GetUpperBound(0)
If Not String.IsNullOrEmpty(varArray(vari, 1)) Then
varCount = varCount + 1
Else
Exit For
End If
Next
Try
varArray = Nothing
varArray = varSheet.Range("A1:L" & varCount).Value
For vari As Integer = 2 To varArray.GetUpperBound(0)
varCount = vari
Dim varSec = funGetSec("mytable")
Dim varTemp As New TEMPORAL
varTemp.ID_TEMPORAL = varSec.NUMERO_SECUENCIAL
If Not varArray(vari, 1) Is Nothing Then varTemp.CEDSOC = varArray(vari, 1).ToString.Trim.ToUpper
If Not varMatrizDatos(vari, 2) Is Nothing Then varTemp.DFCOPR = varArray(vari,2).ToString.Trim.ToUpper
If Not varArray(vari, 3) Is Nothing Then varTemp.NOMSOC = varArray(vari, 3).ToString.Trim.ToUpper
If Not varArray(vari, 4) Is Nothing Then varTemp.DF_FCR = varArray(vari, 4).ToString.Trim.ToUpper
If Not varArray(vari, 5) Is Nothing Then If IsNumeric(varArray(vari, 5)) Then varTemp.DFTOVA = CDec(varArray(vari, 5))
If NotvarArray(vari, 6) Is Nothing Then If IsNumeric(varArray(vari, 6)) Then varTemp.DFINTE = CDec(varArray(vari, 6))
If Not varArray(vari, 7) Is Nothing Then If IsNumeric(varArray(vari, 7)) Then varTemp.K_FALTANTE0 = CDec(varArray(vari, 7))
If Not varArray(vari, 8) Is Nothing Then If IsNumeric(varArray(vari, 8)) Then varTemp.CARDIA = CLng(varArray(vari, 8))
If Not varArray(vari, 9) Is Nothing Then If IsNumeric(varArray(vari, 9)) Then varTemp.DFNUVA = CLng(varArray(vari, 9))
If Not varArray(vari, 10) Is Nothing Then If IsNumeric(varArray(vari, 10)) Then varTemp.BFBECD = CLng(varArray(vari, 10))
If Not varArray(vari, 11) Is Nothing Then varTemp.BFBPTX = varArray(vari, 11).ToString.Trim.ToUpper
If Not varArray(vari, 12) Is Nothing Then varTemp.A0BFTX = varArray(vari, 12).ToString.Trim.ToUpper
varTemp.BFA0CD = CLng(66)
varTemp.FECHA = CDate(Now.Date)
varEntidades.AddToTEMPORALSet(varTemp)
varEntidades.SaveChanges()
Next
varExcel.DisplayAlerts = False
varBook.Close(SaveChanges:=False)
varExcel.Quit()
varSheet = Nothing
varBook = Nothing
varExcel = Nothing
' Exec Stored Procedure '
Dim varResp As Long
Dim varABC As ABC = GetABC(parIDABC)
If Not varEntidades.spABC(parIDABC, varResp, varErrMensaje) Then
If Not varEntidades.spErrorABC(varErrMensaje) Then Throw New ArgumentException(varErrMensaje)
Throw New ArgumentException(varErrMensaje)
End If
Catch ex As Exception
varExcel.DisplayAlerts = False
varBook.Close(SaveChanges:=False)
varExcel.Quit()
varSheet = Nothing
varBook = Nothing
varExcel = Nothing
If Not varEntidades.spErrorABC(varErrMensaje) Then Throw New ArgumentException(varErrMensaje)
Throw New ArgumentException(ex.Message & " - Review Excel file - row number: " & varCount)
End Try
Return Something
I hope this help someone else too.
I cretaed a package using SSIS. This is a good solutions and from code I send parameters to Package (Package.dtsx)
The bulk load is reduced amazingly :)
You can use either an Excel or SQL Server reference in-line on an Excel or SQl Server connection, for example:
You will probably need to enable ad hoc queries on SQL Server and if you are using ACE there may be some other considerations: