tags:

views:

42

answers:

1

I was inserting bulk data from C# code using sqlbulkcopy.There were 15000 records in temp_upload table.Now somehow the datatable in WriteToServer() method had just one column and 37 rows.

After running it I found that the table just had 37 records.Initially it had 152 columns but after this only 32 columns were left.

What could be the reason for this?

C# code for this

public static void BulkInsert(SqlConnection connection,DataTable DtRecord,string   TableName)
    {

        if (DtRecord == null) throw new ArgumentNullException("dataTable");
        // Create & open a SqlConnection, and dispose of it after we are done

            connection.Open();
            SqlBulkCopy bulkCopy = new SqlBulkCopy(connection);
            bulkCopy.DestinationTableName = TableName;
            for (int recordLoop = 0; recordLoop < DtRecord.Columns.Count; recordLoop++)
            {
                bulkCopy.ColumnMappings.Add(DtRecord.Columns[recordLoop].ColumnName, DtRecord.Columns[recordLoop].ColumnName);
            }

            bulkCopy.WriteToServer(DtRecord);
            bulkCopy.Close();
            connection.Close();

    }
A: 

The example code given at this MSDN page has scripts that create tables with the appropriate fields prior to performing the copy operation, so I doubt that SqlBulkCopy changes the schema. Further, SQLBulkCopy utilizes column mappings, which would be inconsistent with changing the schema of the target tables, since mappings would not be needed to do that.

Robert Harvey