Index value issue in IDataReader implementation

c# idatareader sqlbulkcopy

Question

IDataReader was used in a class I built using SqlBulkCopy in a project. Here, I've shared some of the pertinent code.

public bool Read()
{
    var result = !fileStream.EndOfStream;
    if (result)
    {
        delimRow = fileStream.ReadLine();    
        splitRowValues = delimRow.Split(_delimiters);
        readRowCount++;
    }    
    return result;
}

private string[] Row
{
    get { return splitRowValues; }
}

public object GetValue(int i)
{
    return Row[i];
}

Using IDataReader and SqlBulkCopy in certain code

SqlBulkCopy bulkInsert = new SqlBulkCopy(Constants.DBConnection, SqlBulkCopyOptions.UseInternalTransaction);
bulkInsert.BatchSize = 500;
bulkInsert.DestinationTableName = Constants.DestinationTable;
bulkInsert.WriteToServer(reader);

When the WriteToServer function is executed, I've seen that the GetValue() method's initial call begins with the index value set to 1, and the index's final value is one less than the number of columns. This problem results in the bulk load omitting one column.

Why the index in GetValue() does not accept values from 1 to the column count is a mystery to me. Actually, the value should have begun at 0 and decreased by one for every additional column. If someone can point me in the right direction, please let me know.

1
0
11/17/2017 11:26:43 PM

Accepted Answer

The mapping is not specified by you. so naturallySqlBulkCopy may attempt to use the database's automap by ordinal.

For instance, if your database's first column is an identification column (which is often the case), it will skip theindex 0 then begin with theindex 1 instead.

0
11/18/2017 5:29:57 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow