troubleshooting Question

Batch update in C#

Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates asked on
C#
5 Comments1 Solution1991 ViewsLast Modified:
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);
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros