2

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);
}
9
  • possibly a transaction issue? what is db? Commented Dec 13, 2012 at 20:01
  • Either transaction, or no rows in the table, or you're looking at the wrong table in SSMS. Commented Dec 13, 2012 at 20:03
  • database is MSSQL 2008, i am connecting to it as administrator (it's only a test database) and noone else. So i don't think it's a problem with transactions :( Commented Dec 13, 2012 at 20:04
  • I just did some work with bulk copy and had no problems .. +1 to see what the outcome is...please update if you find solution Commented Dec 13, 2012 at 20:05
  • of course i will post a update if i find a solution, it's very weird that i get no errors but no data appears in database Commented Dec 13, 2012 at 20:07

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I actually was suspicious if NewRow was enough but I'm not familiar with data tables :) Glad you found it.
yes this is the first time i use data tables and from other places i'm got used to the thing, when you call NewRow() function it creates a new row and add it to the datatable and returns a pointer to the row so i can update the row :)
@ohigho_state of course, this was just a stupid error on my side :) but i will leave this question here when somebody makes the same error so he can figure it out :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.