Link to home
Create AccountLog in
Avatar of mjasic
mjasic

asked on

How do I make working SqlBulkCopy in VB.NET?

We have a txt file (size around 2 G) that loads very slowly > 8 hrs. So, we decided to use bulk copy to accelerate the process.

This txt file has some special headers (the second line contains the actual column headers) and one line of footer.

First, I don't know how to get rid of the first line of header and the last one (footer) in StreamReader.

Second, when I cut these lines manually (it is not easy with 2 G so in my test I used only 500 lines) and process,
there is an error message stating as follows:
System.InvalidOperationException was unhandled
  Message="The given ColumnName 'BIN_LOC' does not match up with any column in data source."
  Source="System.Data"

The column BIN_LOC exists in my destination file!

What is wrong with the code and how to fix the problem of header/footer?
Thanks for your time.

Mike
Sub BulkImport()
 
        Const strDestTable As String = "[SCM-OBJ-12_input_im_ftn]"
        Const strFilename As String = "C:\Mydata.txt"
 
        Dim i As Long = 0
        Dim dbConn As SqlConnection = New SqlConnection("server=MyServer;uid=MyName;pwd=MyPass;database=MyDB")
        Dim sr As StreamReader = New StreamReader(strFilename)
        Dim line As String = sr.ReadLine()
        Dim strArray As String() = line.Split("~")
 
 
        Dim dt As DataTable = New DataTable()
        Dim row As DataRow
 
        For Each s As String In strArray
            dt.Columns.Add(New DataColumn())
        Next
 
        Do
            row = dt.NewRow()
            row.ItemArray = line.Split("~")
            dt.Rows.Add(row)
            i = i + 1
            line = sr.ReadLine()
 
        Loop While Not line = String.Empty
 
        Dim bc As SqlBulkCopy = New SqlBulkCopy(dbConn, SqlBulkCopyOptions.TableLock, Nothing)
        bc.DestinationTableName = strDestTable
 
        bc.BatchSize = dt.Rows.Count
        bc.ColumnMappings.Add("BIN_LOC", "BIN_LOC")
        bc.ColumnMappings.Add("MATL_TYPE_IN", "MATL_TYPE_IN")
        bc.ColumnMappings.Add("MFG_PART_NO", "MFG_PART_NO")
        bc.ColumnMappings.Add("LOC_PRM_IN", "LOC_PRM_IN")
        bc.ColumnMappings.Add("LOC_SCND", "LOC_SCND")
        bc.ColumnMappings.Add("CUST_ID_IN", "CUST_ID_IN")
        bc.ColumnMappings.Add("CO_NO", "CO_NO")
        bc.ColumnMappings.Add("MFR_PART_NBR", "MFR_PART_NBR")
 
        dbConn.Open()
        bc.WriteToServer(dt)
        dbConn.Close()
        bc.Close()
End Sub
 
--------------------------------------------------------------------
Here is a sample of the beginning of the file:
 
SCM-OBJ-12~SCM-OBJ-12_IM_FTN_INPUT~2008-06-10~0509
BIN_LOC~MATL_TYPE_IN~MFG_PART_NO~LOC_PRM_IN~LOC_SCND~CUST_ID_IN~CO_NO~MFR_PART_NBR~FTN_NBR~MATL_TYPE_FTN~STORG_LOC_ID~FTN_NBR_LAST~RECD_DT~SVC_DT~MFG_CODE~OH_PRICED_QTY~DEFAULT_UPR~MFR_CD~LOC_PRM_FTN~PME_NBR~CUST_ID_FTN~MTC_EVT_NM~RTN_FOR_INSPEC_DT~SER_NBR~MFR_DT~FTN_STS_CD~SER_NBR_TYP_CD~ITEM_CLASS_CODE~ITT_CUST_ID~ITT_MATL_TYPE~ITT_LOC_PRM~ITT_STORG_LOC_ID
                  ~  ~3555111-4                 ~     ~          ~  ~PP1 ~3555111-7                 ~R8Z0L1~  ~177-22 11 ~      ~20070307~        ~99193~ 000000000.0000~ 00000000.01000~99193~ABC  ~               ~PP~               ~        ~    060122212409~        ~ANS~M~0702~  ~  ~     ~

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Here are some thoughts:

1) If the columns names are the same between the source and target, then you don't need column ColumnMappings.

2) The SqlBulkCopy should handle the connection itself, so you don't need to open and close the connection.

3) You are adding columns to a DataTable without any column names.
Avatar of mjasic
mjasic

ASKER

Thanks for the ideas, but the problem is still there.
Friday, I did some changes to my code because I noticed that the column names weren't there, so it isn't an issue any more.

After executing the new code, I had another error:
"The given value of type String from the data source cannot be converted to type nchar of the specified target column."

Today, I tried without connection (as you suggested), but it didn't work. I had to open the connection.

You'll find attached the last version of my code, so I'll appreciate if you have any other idea how to fix the problems with BulkCopy.
Also, how could I skip or cut thefirst and the the last line of data.? Is there any option in BulkCopy to to begin at the second line (the header) and go until the last line -1? In my tests I cut manually these lines (sample of 500 lines), but I cannot do it in a complete file of >2 million lines and then save it. I think it would take hours just to save the file.

Thanks

Mike





        Const strDestTable As String = "[SCM-OBJ-12_input_im_ftn]"
        Const strFilename As String = "C:\Mydata.txt"
        Dim i As Long = 0
        Dim dbConn As SqlConnection = New SqlConnection("server=MyServer;uid=MyName;pwd=MyPass;database=MyDB")
        Dim sr As StreamReader = New StreamReader(strFilename)
        Dim line As String = sr.ReadLine()
        Dim strArray As String() = line.Split("~")
        Dim dt As DataTable = New DataTable()
        Dim row As DataRow
        Dim dc As DataColumn
 
        For Each s As String In strArray
            dc = New DataColumn
            dc.ColumnName = Trim(s)
            dt.Columns.Add(dc)
        Next
 
        Do
 
            row = dt.NewRow()
            row.ItemArray = line.Split("~")
            dt.Rows.Add(row)
            i = i + 1
            line = sr.ReadLine()
 
        Loop While Not line = String.Empty
 
 
        Dim bc As SqlBulkCopy = New SqlBulkCopy(dbConn, SqlBulkCopyOptions.TableLock, Nothing)
        bc.DestinationTableName = strDestTable
        bc.BatchSize = dt.Rows.Count
        dbConn.Open()
        Try
            bc.WriteToServer(dt)    ' <--- the error haapens here
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
        dbConn.Close()
        bc.Close()

Open in new window

1) I wasn't saying that you didn't need the connection, I just said that you don't need to manage the state.

2) If you want to skip the first line, just call ReadLine before you start in the Do loop.  

3) Can you change the column type for nchar to nvarchar?
Avatar of mjasic

ASKER

For 2), I did it before the first Split (before For..Next). Great!
What about the last line?

For 3) - Unfortunatelly, I am not the one that defined the SQL tables and (also unfortunatelly) all the fields in all tables are "nchar". The tables were generated by a tool that a member of our team developped two years ago. Is there anything we can do for this?

Thanks


2) For the last line, I would just remove the last DataRow from the DataTable that you are building.

3) You should be able to use something like SQL Server Management Studio, or Server Explorer, to modify the column type to change if from nchar to nvarchar.

Avatar of mjasic

ASKER

Unfortunatelly, I am not allowed to change the data types in tables (and, moreover, there are several hundreds of tables in our staging system so it would be too long). I must do something with the tables (and their structures) I have. I tried the casting and some other things, but it wasn't better :(
Is there any other way to fix this problem?
Avatar of mjasic

ASKER

I just did a test. I recreated on table (under other name) and changed nchar to nvarchar. The error is:
The given value of type String from the data source cannot be converted to type nvarchar of the specified target column.

So, it must be something else :(
Hmmm...I usually don't use nvarchar, unless I am using it for a very specific reason, since it is 2 bytes per character (globalization), but I would have thought that the SqlBulkCopy could have handled the conversion--I have been wrong before, and I will probably be wrong again in the future about something else.
ASKER CERTIFIED SOLUTION
Avatar of mjasic
mjasic

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
So what was the solution?