Question : VB.Net - BulkCopy Excel to SQL...works, but funky

Experts, the VB.Net code in the snippet below successfully bulkcopys data from Excel into a SQL Table, however for some reason it places the data in rows 66 & 67 and leaves out the top row of data...does it reserve that for a header?

Please help me figure out how to get the Excel data populated in the top left corner of the SQL Table, similarly to how it is in the Excel worksheet.

Details:
- The Excel data consists of 3 rows and 2 columns of 3 to 10 digit numbers...hand keyed in (sample data)
- The Excel data exists in rows 1:3 and columns A:B with now headers, just straight datavalues
- The SQL Table is pre-populated with 100 columns with 100 rows of null data (DBNull.Value)
- The SQL Table datatype is "double precision"
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Private Sub continueimprtBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles continueimprtBTN.Click
        'IMPORT DATA FROM EXCEL INTO SQL DB MODULE TABLE
       
        'CONNECTION STRING TO EXCEL WORKBOOK
        Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Adam\Desktop\easydata.xls;Extended Properties=""Excel 8.0;HDR=YES;"""
 
        'CREATE CONNECTION TO EXCEL WORKBOOK
        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()
                'BULK COPY TO SQL SERVER
                Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(MySharedConnExisting)
                    bulkCopy.DestinationTableName = "dbo." & moduletableCB.Text
                    bulkCopy.WriteToServer(dr)
                End Using
            End Using
            If MySharedConnExisting.State = ConnectionState.Open Then
                MySharedConnExisting.Close()
            End If
        End Using
    End Sub

Answer : VB.Net - BulkCopy Excel to SQL...works, but funky

I did find some references that say that the Excel Sheet and the Table Definition need to match exactly.  The only way you could do this would be to add blank columns to your excel dataset before the BulkLoad.  I know you can do this with a SQLDataSet but don't know if you could do it with an OleDBDataReader.

Or, you can export out the .XLS file to a Tab Delimited Text File, and insert extra tabs to match the number of columns in the database table.  Then read the Tab Delimited Text File as input to the SqlBulkLoad.

Creating a table with thousands of rows and hundreds of empty columns is a very inefficient design.  A better design would be to create an empty table in the database after the app user sets up their table structure (so you know how many columns to create), not to create a large table that can work for everyone... at least if you want to use the SqlBulkCopy command.  

Not sure why you are inserting rows with NULL values into the table before the Bulk Load.  Bulk Load will just append to the table IIRC.  You're just wasting space (even rows/columns with NULL values still take up space).
Random Solutions  
 
programming4us programming4us