How to Import an Excel File Into SQL Server 2000 Using .NET?

Good Day:

I am creating a WinForms application using VB.NET 2005 with SQL Sever 2000.  One of the Experts from Experts Exchange gave me the following code to create a DataSource from an Excel file. My question is how do I perform a batch insert into my SQL Server table called Quote?  Or is there a better way of importing an Excel file into SQL Sever 2000 using .NET?  Or should I put the Excel file in a grid and try to iterate through the rows to insert the Excel data into SQL Server?


Private Function GetdatafromExcel(ByVal pFilename As String, ByVal pRangeName As String) As System.Data.DataSet
        'Returns dataset from named range
        Dim l_strConn As String = String.Format("{0}{1}{2}{3}", _
                                                "Provider=Microsoft.Jet.OLEDB.4.0;", _
                                                "Data Source=", _
                                                pFilename, _
                                                ";Extended Properties=Excel 8.0;")

        Dim l_Conn As New OleDbConnection(l_strConn)

        'Create Objects and grab data
        Dim l_Cmd As New OleDbCommand("SELECT * FROM " & pRangeName, l_Conn)

        Dim l_DA As New OleDbDataAdapter()
        l_DA.SelectCommand = l_Cmd

        'Fill Dataset
        Dim l_DS As New DataSet()

        'Clean Up and Return
        Return l_DS

    End Function
Who is Participating?
FMabeyConnect With a Mentor Commented:
I have used DTS packages like you are trying to by copying the file for import and renaming it placing it in a folder on the users computer. This way it is always called the same and always in the same place.

Here's my code:

' Copy the file that the user wishes to import into the required location
        Dim STR_file1 As String

        If OFD_import.ShowDialog() = DialogResult.OK Then

            Dim STR_FilePathAndName As String = OFD_import.FileName

            STR_file1 = "C:\Import\Import.txt"

            ' Remove the file from the import folder - this file cannot exist a new one is copied over
            If System.IO.File.Exists(STR_file1) = True Then


            End If

            ' Copy the file to the import folder
            If System.IO.File.Exists(STR_FilePathAndName) = True Then

                System.IO.File.Copy(STR_FilePathAndName, STR_file1)

            End If


            ' Run the DTS package
            Dim oPackage As DTS.Package = New DTS.Package()
            oPackage.LoadFromSQLServer( _
                ServerName:="SQLSVR1", _
                Flags:=DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, _
                PackageName:="Part Manager Import")
            oPackage = Nothing

Any queries please ask.

koolnurdConnect With a Mentor Commented:
Create a new SSIS package.  Make the Excel file the source document and the the new table the destination tablename.
DeniseGoodheartAuthor Commented:
Thanks for your suggestion but I do not think I can use the DTS Package because many users will be importing different file names from their computer.  I believe the DTS works when it has one specific file name at one specific directory location.  
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

DeniseGoodheartAuthor Commented:
Perhaps I need to start a new thread and ask about how to do a bulk insert using VB.NET; since it has the System.Data.SqlClient namespace called SqlBulkCopy.  
Have you tried my suggestion? It works fine for me and I think it will do what you want.
DeniseGoodheartAuthor Commented:
What happens when 2 users are running the DTS at the same time?
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
simplest way out for me will be to copy the file somewhere on the network (shared folder which is accessible from the sql server machine) and the use the follwoing qyery (dynamically by changing the filename and sheet name if its different) to import the data
select *
 from openrowset('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\report\data.xls;HDR=YES', 'select * from [Sheet1$]')
DeniseGoodheartAuthor Commented:

Running DTS on VB.NET requires a DTS Package name.  The DTS Package imports a specific fille name from a specific location.  By changing the names of the file; DTS will not be able to import it.
Anurag ThakurTechnical ManagerCommented:
this is not a DTS
its a openrowset which you can call from a stored procedure
The code I've supplied you will change the name of the file so that it will always be the same. Therefore, you will not have an issue with file names being different as you will always change the filename to be the same.
2 users at the same time? That I'm not to sure about.
You could, as you have mentioned, use bulk copy.

Here is a sample of the sort of code you'd require:

DECLARE @bcpCommand VARCHAR(8000)

SET @bcpCommand = 'bcp partman.dbo.vw_export_workord out c:\PartManExport\WorkOrders.txt -S SQLSVR1 -T  -c'
EXEC master..xp_cmdshell @bcpCommand

This copies info from a sql server table and into a text file. You simply need to reverse this.
DeniseGoodheartAuthor Commented:

Perhaps you can show me your entire code so I understand more.

DeniseGoodheartAuthor Commented:

Thanks for your code.  I will have to do some experimenting.
Anurag ThakurTechnical ManagerCommented:
sorry for the delayed response

what you can do is when the user uploads the excel file for data import you can copy/move the file to a particular directory from where the sql server machine has read access to that folder and save the full path of the XLS file somewhere

you can then create a stored procedure and pass in the full path name of the xls file and what ever other required parameters to the stored procedure

in the stored procedure you can create a dynamic sql statement and execute it as shown below
if you want to directly insert into the required table then you can modify the sql statement directly

set quoted_identifier off
declare @SQL VarChar(1000)
Select @SQL = "select * from openrowset('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=" + @Path +";HDR=YES', 'select * from [Sheet1$]')"
execute (@sql)

i will be glad to help if there are anything else
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.