G
Guest
Hi Experts,
I have huge fixed width text files around 9GB or 60 TO 75 millions records.
I need to transfer 300GB data from these Text files to SQL Server 2000
monthly bases. I am trying to develop VB.NET application. One problem is I
need to transform this data, like date is 6 char (mmddyy) which needs to be
convert into real Date (mm/dd/ccyy) during the import. I tried to use
datareader to bring data into DataTable and then append data into SQL Server
using SQLBulkInsert but its taking long time and start failing after 10-16
millions records appended into SQL Server 2000 table.
I have following questions:-
1- Which class-method should I use to read the data from text file?
2- How to perform Transformation data ( like converting 6 char date fields
into DATE and other validations)?
3- Which Class-method should I use to append data into SQL Server 2000 to
get the best performance?
4- Is there any better way to done the same job?
I am using VB.NET 2005 Beta.
Thank you so much for your time and help.
Some code what I am using right now to do so
Using rdrDataFile As New TextFieldParser(mstrDataFilePath)
rdrDataFile.TextFieldType = FileIO.FieldType.FixedWidth
rdrDataFile.SetFieldWidths(objDataFile.SplitPositions)
arrNumToExclude.AddRange(objDataFile.ColumnIndicesToExclude)
tblDataFile = objDataFile.StagingTable
Dim currentRow As String()
Dim rowIndex As Integer = 0
destConn = New SqlConnection(strConnectionString)
'Creates Connection
destConn.Open() 'Opens Connection
' intTotalRowCount = 1
While Not rdrDataFile.EndOfData
Try
Dim myRow As DataRow = tblDataFile.NewRow
currentRow = rdrDataFile.ReadFields()
Dim currentField As String
Dim columnIndex As Integer = 0
Dim intCounter As Integer = 0
For Each currentField In currentRow
If Not arrNumToExclude.Contains(intCounter)
Then
myRow(columnIndex) =
objDataFile.FormatData(columnIndex, currentField)
columnIndex += 1
End If
intCounter += 1
Next
tblDataFile.Rows.Add(myRow)
Catch ex As Exception
End Try
rowIndex += 1
If rowIndex = 100000 Or rdrDataFile.EndOfData Then
'Upload data To Database in Batches
BulkCopyToDB(tblDataFile,
objDataFile.DestinationTable) 'Copy Data into DB
tblDataFile.Clear()
rowIndex = 0
End If
'vpbDataFile.Value = IIf(intTotalRowCount <
vpbDataFile.Maximum, intTotalRowCount, vpbDataFile.Value)
'intTotalRowCount += 1
End While
End Using
Public Sub BulkCopyToDB(ByVal vtblDataFile As DataTable, ByVal vstrDestTable
As String)
Dim bcp As SqlBulkCopy = Nothing
Dim strConnectionString As String = ""
Try
bcp = New SqlBulkCopy(destConn)
bcp.DestinationTableName = vstrDestTable
bcp.WriteToServer(vtblDataFile)
Catch ex As Exception
Finally
If Not bcp Is Nothing Then
bcp.Close()
End If
End Try
End Sub
Regards,
-Permood
I have huge fixed width text files around 9GB or 60 TO 75 millions records.
I need to transfer 300GB data from these Text files to SQL Server 2000
monthly bases. I am trying to develop VB.NET application. One problem is I
need to transform this data, like date is 6 char (mmddyy) which needs to be
convert into real Date (mm/dd/ccyy) during the import. I tried to use
datareader to bring data into DataTable and then append data into SQL Server
using SQLBulkInsert but its taking long time and start failing after 10-16
millions records appended into SQL Server 2000 table.
I have following questions:-
1- Which class-method should I use to read the data from text file?
2- How to perform Transformation data ( like converting 6 char date fields
into DATE and other validations)?
3- Which Class-method should I use to append data into SQL Server 2000 to
get the best performance?
4- Is there any better way to done the same job?
I am using VB.NET 2005 Beta.
Thank you so much for your time and help.
Some code what I am using right now to do so
Using rdrDataFile As New TextFieldParser(mstrDataFilePath)
rdrDataFile.TextFieldType = FileIO.FieldType.FixedWidth
rdrDataFile.SetFieldWidths(objDataFile.SplitPositions)
arrNumToExclude.AddRange(objDataFile.ColumnIndicesToExclude)
tblDataFile = objDataFile.StagingTable
Dim currentRow As String()
Dim rowIndex As Integer = 0
destConn = New SqlConnection(strConnectionString)
'Creates Connection
destConn.Open() 'Opens Connection
' intTotalRowCount = 1
While Not rdrDataFile.EndOfData
Try
Dim myRow As DataRow = tblDataFile.NewRow
currentRow = rdrDataFile.ReadFields()
Dim currentField As String
Dim columnIndex As Integer = 0
Dim intCounter As Integer = 0
For Each currentField In currentRow
If Not arrNumToExclude.Contains(intCounter)
Then
myRow(columnIndex) =
objDataFile.FormatData(columnIndex, currentField)
columnIndex += 1
End If
intCounter += 1
Next
tblDataFile.Rows.Add(myRow)
Catch ex As Exception
End Try
rowIndex += 1
If rowIndex = 100000 Or rdrDataFile.EndOfData Then
'Upload data To Database in Batches
BulkCopyToDB(tblDataFile,
objDataFile.DestinationTable) 'Copy Data into DB
tblDataFile.Clear()
rowIndex = 0
End If
'vpbDataFile.Value = IIf(intTotalRowCount <
vpbDataFile.Maximum, intTotalRowCount, vpbDataFile.Value)
'intTotalRowCount += 1
End While
End Using
Public Sub BulkCopyToDB(ByVal vtblDataFile As DataTable, ByVal vstrDestTable
As String)
Dim bcp As SqlBulkCopy = Nothing
Dim strConnectionString As String = ""
Try
bcp = New SqlBulkCopy(destConn)
bcp.DestinationTableName = vstrDestTable
bcp.WriteToServer(vtblDataFile)
Catch ex As Exception
Finally
If Not bcp Is Nothing Then
bcp.Close()
End If
End Try
End Sub
Regards,
-Permood