Search code examples
c#datatable

What is the fastest way to load data into a DataTable?


At this Link I have found my new fast(faster than Rows.Add()) implemetation for adding data to a new DataTable (or at list i thought so ... (: for a minute there )

I remember reading somewhere that ImportRow() is quickest though it seems that in that way using ImportRow does not updtae the table rows, is it like DataTable.Clone() ?

What is the fatest way to implement multiple rows Loaded into a table from custom data in a loop ?

var returnedDtFromLocalDbV11 =  DtFromDb(strConnName, queryStr, strReturnedDtName);
NewDtForIns = returnedDtFromLocalDbV11.Clone();
Stopwatch SwSqlMdfLocalDb11 = new Stopwatch();
SwSqlMdfLocalDb11.Start();
NewDtForIns.BeginLoadData();

for (int i = 0; i < 1000000; i++)
{
   var Dr = NewDtForIns.NewRow();
   Dr[tblClients.LastName] = string.Concat(i, "_"+lastNameStr);
   Dr[tblClients.FirstName] = string.Concat(i,  "_" +firstNameStr);
   //NewDtForIns.Rows.Add(Dr) ;
   //NewDtForIns.ImportRow(Dr) ;
   NewDtForIns.LoadDataRow(new object[] { null, "NewShipperCompanyName", "NewShipperPhone" }, false);
}

 NewDtForIns.EndLoadData();
 DBRCL_SET.UpdateDBWithNewDtUsingSQLBulkCopy(NewDtForIns, tblClients._TblName);
 SwSqlMdfLocalDb11.Stop();
 var ResSqlMdfLocalDbv11_0 = SwSqlMdfLocalDb11.ElapsedMilliseconds;

Update

turns that it is faster ! my mistake i forgot to comment the 3 datarows Lines that takes time to generate while testing the example with LoadDataRow()

   //var Dr = NewDtForIns.NewRow();
   //Dr[tblClients.LastName] = string.Concat(i, "_"+lastNameStr);
   //Dr[tblClients.FirstName] = string.Concat(i,  "_" +firstNameStr);

now the results are for that test in my configuration is :

Rows.Add()        => ~6700ms
LoadDataRow()     => ~5200ms - 5500ms

that is nice but i was hoping for little more performance difference via ImportRow()

any suggestions ?


Solution

  • as @Magnus suggested this is the faster way to populate the DataTable with new Rows nice and simple fewer lines of code

    Stopwatch SwSqlMdfLocalDb11 = new Stopwatch();
    SwSqlMdfLocalDb11.Start();
    NewDtForIns.BeginLoadData();
    
    for (int i = 0; i < 1000000; i++)
    {
       NewDtForIns.LoadDataRow(new object[] { null, "NewShipperCompanyName", "NewShipperPhone" }, false);
    }
    
     NewDtForIns.EndLoadData();
     DBRCL_SET.UpdateDBWithNewDtUsingSQLBulkCopy(NewDtForIns, tblClients._TblName);
     SwSqlMdfLocalDb11.Stop();
    

    i will be happy to learn a new approach if there is one, although that's already some news for me... thanks.