Im having strange problems when importing data from an excel file into a sql table
Everything seems to imports ok until i look at the data in more detail, this is when I notice its imported the data but in the wrong order.
for some reason when I look at the db table all records are in the wrong order, and strangely enough it seems to be a different order every time I import things.
Is there a way to force the routine to import the data in the same order as the it is in the excel file?, because at the minute it seems to have a mind of its own
any help / advice will be appreciated as always
thanks
My code is below:
Public Function ReadExcelFile(ByVal sFileName As String) As Boolean
Dim oSQL As New clsSQLHolder
Dim excelCon As String
Dim excelstrCon As OleDb.OleDbConnection
Dim reader As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand
Try
Try
excelCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFileName + ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
excelstrCon = New OleDb.OleDbConnection(excelCon)
excelstrCon.Open()
'Create objects and get data
cmd = New OleDb.OleDbCommand("SELECT * FROM [Sheet1$]", excelstrCon)
reader = cmd.ExecuteReader
'open the destination data
Dim dbCon As SqlConnection = New SqlConnection(sConnectionString)
dbCon.Open()
Dim bulkCopy As SqlBulkCopy = New SqlBulkCopy(dbCon)
bulkCopy.DestinationTableName = "tblFocusTemp"
bulkCopy.WriteToServer(reader)
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
Finally
reader.Close()
excelstrCon.Close()
End Try
End Function
Everything seems to imports ok until i look at the data in more detail, this is when I notice its imported the data but in the wrong order.
for some reason when I look at the db table all records are in the wrong order, and strangely enough it seems to be a different order every time I import things.
Is there a way to force the routine to import the data in the same order as the it is in the excel file?, because at the minute it seems to have a mind of its own
any help / advice will be appreciated as always
thanks
My code is below:
Public Function ReadExcelFile(ByVal sFileName As String) As Boolean
Dim oSQL As New clsSQLHolder
Dim excelCon As String
Dim excelstrCon As OleDb.OleDbConnection
Dim reader As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand
Try
Try
excelCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFileName + ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
excelstrCon = New OleDb.OleDbConnection(excelCon)
excelstrCon.Open()
'Create objects and get data
cmd = New OleDb.OleDbCommand("SELECT * FROM [Sheet1$]", excelstrCon)
reader = cmd.ExecuteReader
'open the destination data
Dim dbCon As SqlConnection = New SqlConnection(sConnectionString)
dbCon.Open()
Dim bulkCopy As SqlBulkCopy = New SqlBulkCopy(dbCon)
bulkCopy.DestinationTableName = "tblFocusTemp"
bulkCopy.WriteToServer(reader)
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
Finally
reader.Close()
excelstrCon.Close()
End Try
End Function