Debugging SqlBulkCopy problems when it doesn't return any errors

c# sqlbulkcopy

Question

Right now my problem with sqlBulkCopy is that it finishes without any errors but when i look into the table i can't see any data.

The try catch block catches no errors and am i missing something? Because i don't see any problems with my code

This is the code i use:

DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Buying ID", typeof(string)));
table.Columns.Add(new DataColumn("Buying BPC ID", typeof(string)));
table.Columns.Add(new DataColumn("Buying BP Name", typeof(string)));
table.Columns.Add(new DataColumn("Buying BP Post Code", typeof(string)));
... snip ...
table.Columns.Add(new DataColumn("Buyer BP Type", typeof(string)));
table.Columns.Add(new DataColumn("Seller BP Type", typeof(string)));
table.Columns.Add(new DataColumn("FileSource", typeof(string)));
table.Columns.Add(new DataColumn("ImportDate", typeof(System.DateTime)));

while ( (line = sr.ReadLine()) != null)
{   
    string[] data = line.Split('\t');
    DataRow row = table.NewRow();
    for (int i=0; i<data.Length; i++)
    {
        row[i] = data[i];
    }
    row["FileSource"] = fi.Name;
    row["ImportDate"] = DateTime.Now;
}

try
{
    db.Connection.Open();
    SqlBulkCopy bulkcopy = new SqlBulkCopy(db.Connection);
    bulkcopy.BatchSize = 500;
    bulkcopy.BulkCopyTimeout = 600;
    bulkcopy.DestinationTableName = "dbo.LNST_test";
    foreach (var col in table.Columns)
    {
        bulkcopy.ColumnMappings.Add(col.ToString(), col.ToString()); //col names in datatable are identical to col names in database
    }
    bulkcopy.WriteToServer(table);
    db.Connection.Close();
}
catch (Exception e)
{
    Console.WriteLine("ERROR : "+e.Message);
}

Accepted Answer

Oh god i'm stupid... i forgot this one line: table.Rows.Add(row);

I tried to insert an empty datatable. This is why i love stackoverflow, even if i don't get the answer from this avesome community, it helps me to solve my problems :D



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