Results 1 to 9 of 9

Thread: Import Excel data into SQL Server

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Import Excel data into SQL Server

    Hi guys,

    how can I import excel data into SQL Server with vb.net?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Import Excel data into SQL Server

    A one time thing or to be done more then once?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Import Excel data into SQL Server

    Hi GaryMazzone,

    Thanks for ur reply, I want to do it at one time..
    I found out a solution by using sqlbulkcopy but i had some issue on how to define the data from the excel file to required field in sql because i been given an excel file without a header

    here is my codes :

    'Connect string to excel
    Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.xls;Extended Properties=""Excel 8.0;HDR=NO;"""

    'connection to excel
    Using connection As OleDbConnection = New OleDbConnection(excelConnectionString)

    Dim myCommand As New OleDbCommand("Select * FROM [Sheet1$]", connection)
    connection.Open()

    '// Create DbDataReader to Data Worksheet
    Using dr As OleDbDataReader = myCommand.ExecuteReader()
    '// SQL Server Connection String
    Dim sqlConnectionString As String = "data source=XPC010\SQLEXPRESS;initial catalog=Sabah;user id=sa;password=12345;persist security info=false"
    '"Data Source=XPC010\SQLEXPRESS;id=sa;Password=12345 Initial Catalog=Sabah;Integrated Security=True"

    '// Bulk Copy to SQL Server
    Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(sqlConnectionString)
    bulkCopy.DestinationTableName = "dbo.Barcode"
    bulkCopy.WriteToServer(dr)
    End Using
    End Using

    End Using

    Hope u can help me on this.
    Thanks in advanced

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Import Excel data into SQL Server

    The easiest way would be to not do it in VB but use SSIS package to import the data. Very quick and actually pretty easy to use.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Import Excel data into SQL Server

    really? but can u provide me the code for importing excel file one at a time using sqlcommand?

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Import Excel data into SQL Server

    Treat Excel as a database.

    1. Open a connection to the execl file (this will be an OLEDB connection).
    2. Read the worksheet like a table in a database.
    3. Use either a datatable/dataset or DataReader to perform step 2.
    4. Close the connection to the Excel workbook
    5. Open a connection to the SQL Server database.
    6. Loop though the set created in step 2 and generate an Insert statement for the data going into the
    SQL Server.
    7. Close the connection to the SQL Server.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Import Excel data into SQL Server

    Code:
    Using con As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0 ; Data Source=database.xls ; Extended Properties=Excel 8.0")
    
                con.Open()
    
                Dim cmd As New OleDbCommand("select * from [sheet1$] where YourColumnName=@columnname", con)
                cmd.Parameters.AddWithValue("@columnname", TextBox1.Text)
                Dim dr As OleDbDataReader
                dr = cmd.ExecuteReader()
    
                dr.Read()
    
               
            End Using
    this is the code that may help you to perofrm the first three steps,ok

    now if you are working with excel then you might be knowing how to do the step 4.

    and for steps 5 to 7......you have to do it yourself since i consider that you are not a beginner

    Now,before posting some queries please check your code twice or thrice as i think there should not be any more question regarding this as gary have shown you the steps of doing it and i have given you an outline idea of how to implement these steps


    If you find my reply helpful , then rate it

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Import Excel data into SQL Server

    Thanks dude..I got it..

  9. #9
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: Import Excel data into SQL Server

    Hi guys... just wondering is I can use any of this to load data in several excel files in a folder into a sql server 2000 table? All the data will be in the first sheets in the excel files and the table will be an existing table with the same columns etc.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width