Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3611932
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:56:11+00:00 2026-05-18T21:56:11+00:00

I have a code that loads data from Excel into SQLServer table. It works

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:56:12+00:00Added an answer on May 18, 2026 at 9:56 pm

    You can use either an Excel or SQL Server reference in-line on an Excel or SQl Server connection, for example:

    SELECT Col1, Col2, Col3 INTO 
    [ODBC;Description=TEST;DRIVER=SQL Server;SERVER=Some\Instance;Trusted_Connection=Yes;DATABASE=test].TableZ  FROM [Sheet1$]"
    

    You will probably need to enable ad hoc queries on SQL Server and if you are using ACE there may be some other considerations:

    SELECT * INTO newx
    FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
        'Excel 8.0;HDR=YES;IMEX=1;Database=c:\docs\testdata.xls',
        'SELECT * FROM [Sheet2$]');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am querying information from Active Directory . I have code that works, but
I have this code that performs an ajax call and loads the results into
I have code that looks like the following, which works fine for displaying the
I have developed a VB.Net code for retrieving data from excel file .I load
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
Following on from this question I now have code that can attach to a
Hashtables have a syncroot property but generic dictionaries don't. If I have code that
I need to import some data from a excel file and a folder with
I have a page that loads a div from another page on successful submission
I have a singleton object called PoolManager that loads and saves some data in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.