Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Batch update in C#

Posted on 2009-12-26
Medium Priority
Last Modified: 2012-05-08

I am working on ASP.net using C#

I wanted to do Batch insert from my stored proc

I wrote below code

But to execute batch command I need DataTable, DataSet etc

Cannot I do this using my object as using my below code

Please help me doing batch update

Many Thanks
public void BatchUpdate(List<TripDetailValuesBO> tripDetailValuesList)
    int batchSize = 20;
    DbConnection dbConn = new DbConnection();
    SqlConnection connection = dbConn.Connection();

    SqlDataAdapter adapter = new SqlDataAdapter();

    foreach (TripDetailValuesBO tripDetailValuesBO in tripDetailValuesList)
	adapter.UpdateCommand = new SqlCommand(
	    "EXEC insertTripData @tripID, @questionID, @answer", connection);
	   SqlDbType.Int, tripDetailValuesBO.tripID);
	   SqlDbType.Int, tripDetailValuesBO.questionID);
	   SqlDbType.VarChar, 8000, tripDetailValuesBO.tripdata_question_answer);
	adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;

    adapter.UpdateBatchSize = batchSize;

Open in new window

Question by:tia_kamakshi
  • 2
  • 2
LVL 11

Expert Comment

ID: 26125583
unfortunately you won't be able to do batch update on your SqlAdapter without using a dataset. If you do need to use a SqlAdapter (and not LinqToSql) and if you have to use a List, then you can try converting your List into a dataset.
here is a thread about this subject: http://stackoverflow.com/questions/564366/generic-list-to-datatable

Author Comment

ID: 26126485
Thanks for your reply.

If I donot wanted to convert my list List<TripDetailValuesBO> to datatable

Then what is the best way to do batch update, If I cannot use SqlAdapter

Also, If I have to do insert query into the database table (using MSSQL 2005), what is the good max batch size I can give

Please suggest
LVL 11

Expert Comment

ID: 26127949
typically, how many records are we talking about for your updates and inserts?

Author Comment

ID: 26128003
Its around 300 to 400 records

Thanks for reply and support
LVL 11

Accepted Solution

ROMA CHAUHAN earned 2000 total points
ID: 26130567

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question