C# Data Table Multiple Rows

You have multiple rows in C# data table and you have to save it in database using just one call, How will you do it?

Questions by manisha.varshney

Editorial / Best Answer

LordAlex  

  • Member Since Nov-2011 | Nov 5th, 2011


In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to: 1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter. 2. Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects. 3. Invoke the GetChanges method to create a second DataSet that features only the changes to the data. 4. Call the Update method of the DataAdapter, passing the second DataSet as an argument. 5. Invoke the Merge method to merge the changes from the second DataSet into the first. 6. Invoke the AcceptChanges on the DataSet. Alternatively, invoke RejectChanges to cancel the changes.

Showing Answers 1 - 27 of 27 Answers

Hiren_2009

  • May 9th, 2010
 

Use foreach loop
foreach(datarow temp in datagridview1.rows)
{
      update ....-query;
}

All is  done with just a single click.

  Was this answer useful?  Yes

LordAlex

  • Nov 5th, 2011
 

In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to:

1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.

2. Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects.

3. Invoke the GetChanges method to create a second DataSet that features only the changes to the data.

4. Call the Update method of the DataAdapter, passing the second DataSet as an argument.

5. Invoke the Merge method to merge the changes from the second DataSet into the first.

6. Invoke the AcceptChanges on the DataSet.

Alternatively, invoke RejectChanges to cancel the changes.


  Was this answer useful?  Yes

indrajith

  • Jan 22nd, 2015
 

We can use SqlBulkCopy class here for example

Code
  1. SqlBulkCopy  bulkCopy=new SqlBulkCopy  (sqlconn);

  2.  bulkCopy.DestinationTableName = "dbtablename";

  3.  bulkCopy.WriteToServer(datatable);

  4.  

  Was this answer useful?  Yes

Vasu

  • Apr 10th, 2015
 

Yes

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions