MySqlBulkLoader .Load method always returns 1

39 views Asked by At

I recently updated one of my utilities to use the MySqlConnector library instead of the MySql.Data and I'm finding a discrepancy that I'm hoping is resolvable, although my Google-fu seems to be failing me. I have a method for loading data into the database from a file using the MySqlBulkLoader class:

Protected Friend Function LoadData(ByVal SourceFile As IO.FileInfo, ByVal DestinationTableName As String) As Long
    If Not Me.DB.DoesTableExist(DestinationTableName) Then
        Me.DB.CreateTable(SourceFile, DestinationTableName)
    End If

    Me.DB.Open()

    Try
        Dim COPYWriter As New MySqlBulkLoader(Me.DB.DBConnection)

        With COPYWriter
            .Local = True
            .TableName = Me.DB.GetQuerySafeTableName(DestinationTableName)
            .FieldTerminator = "\t"
            .LineTerminator = "\n"
            .FileName = SourceFile.FullName.Replace("\", "/")
            .NumberOfLinesToSkip = 0
            Return .Load()
        End With
    Catch ex As MySqlException
        Me.DB.LastException = ex

        If Me.DB.ThrowSQLException Then
            Throw
        End If

        Return -1
    Finally
        If Not Me.DB.LeaveOpen Then
            Me.DB.Close()
        End If
    End Try
End Function

The references to Me.DB are to a wrapper class I have for connecting to the MySQL database on various servers and the .DBConnection property of that object is the MySqlConnection object.

It appears that this LoadData method "works" in the fact that the data is there in the database. However, the return value of the COPYWriter.Load() method always seems to be 1, regardless of how many rows were in the original file.

For example, I have a client.sql file that I'm loading that contains 300 records that look something like this:

AAM0002940  KENNETH STEPHEN COY DDS 152 SHADOW RD   EDMOND  OK  73034   5558239826  \N  1326    \N  QBE
AAM0004753  JANE AND JAMES CRISTO   421 15TH AVE    SOMMERSET   WI  54012   5557458143  \N  1310    \N  QBE

According to the documentation, the .Load() method:

Loads all data in the source file or stream into the destination table.

Returns:
  The number of rows inserted.

My call to the .Load() method in the code above doesn't generate any exceptions, and going into the database after it's completed doesn't show any problem with the data. It just always seems to return a value of 1 instead of the expected value of (in this case) 300.

I tried updating the MySqlConnector library to the latest available version of 2.3.3, but it did not affect this behavior. Am I missing a property setting in my initialization code above? Since there's no exception being thrown and the data doesn't appear to be "incorrect" in the target table, I suppose I'm making an assumption that this behavior and result are coming from the MySqlConnector library.

EDIT:

Well, crap. I just did a test where I had it TRUNCATE the destination table before loading the data again and this time it did, indeed, only insert a single record (causing me a moment of panic).

I switched back to the MySql.Data library and made the somewhat minor adjustments required to various bits of code to account for the differences between the two, and it all started working again. The .Load() method correctly inserted all 300 records and returned the correct value.

I did not change the LoadData method I posted above, so the only change that should be at play here is the way the two different libraries handle the incoming data file itself.

One thing about this utility is that it's actually extracting/dumping data from a PostgreSQL database for re-import into the MySQL database. I know that I had to do some work to get the file from PGSQL to match up and be correctly read into MySQL, so I have to assume at this point that there's something about the structure/format of that file that the MySqlConnector library doesn't like.

0

There are 0 answers