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

x
?
Solved

Batch update in C#

Posted on 2009-12-26
5
Medium Priority
?
1,516 Views
Last Modified: 2012-05-08
Hi,

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);
	adapter.UpdateCommand.Parameters.Add("@tripID",
	   SqlDbType.Int, tripDetailValuesBO.tripID);
	adapter.UpdateCommand.Parameters.Add("@questionID",
	   SqlDbType.Int, tripDetailValuesBO.questionID);
	adapter.UpdateCommand.Parameters.Add("@answer",
	   SqlDbType.VarChar, 8000, tripDetailValuesBO.tripdata_question_answer);
	adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
    }            

    adapter.UpdateBatchSize = batchSize;
    adapter.Update(dataTable);
}

Open in new window

0
Comment
Question by:tia_kamakshi
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:lenordiste
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
0
 

Author Comment

by:tia_kamakshi
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
0
 
LVL 11

Expert Comment

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

Author Comment

by:tia_kamakshi
ID: 26128003
Its around 300 to 400 records

Thanks for reply and support
0
 
LVL 11

Accepted Solution

by:
ROMA CHAUHAN earned 2000 total points
ID: 26130567
0

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