i




 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save DataTable to database c#
01-26-2013, 08:20 PM
Post: #1
Save DataTable to database c#
There are different ways to save the DataTable data to database, but i prefer to use SqlBulkCopy (depend on situation)

so here is the code to save DataTable data to database using SqlBulkCopy

Code:
//pass your SqlConnection to SqlBulkCopy Constructor
SqlBulkCopy bulkcopy = new SqlBulkCopy(ObjSqlServer.Connection);

//set the destination table name (should be in database)
            bulkcopy.DestinationTableName = "Tbl_EmailStatus";

//map your DataTable columns with Database Table columns
//if your source column and destination column are not equal then set the proper source column index and destination column index to save the data on respective columns
                bulkcopy.ColumnMappings.Add(0, 0);
                bulkcopy.ColumnMappings.Add(1, 1);
                bulkcopy.ColumnMappings.Add(2, 2);
                bulkcopy.ColumnMappings.Add(3, 3);
                bulkcopy.ColumnMappings.Add(4, 4);
                bulkcopy.WriteToServer(datatable);




[Image: 2604595733.png]
Quote
01-27-2013, 02:47 PM
Post: #2
RE: Save DataTable to database c#
Great work Nisar. Using this we can make DB utility apps etc... Wink



View Asad Ali's profile on LinkedIn

Quote
01-27-2013, 04:40 PM
Post: #3
RE: Save DataTable to database c#
HI nisar nice to share this answer,but we can also do this using table type variables.

“A mind stretched by a new idea can never go back to its original dimension.”

Quote
01-27-2013, 07:03 PM
Post: #4
RE: Save DataTable to database c#
@praveen, as i already mentioned, there are different ways to save datatable to database. Can you post a resource on how to save datatable into database using table type variables. it will help the users to choose the best solution according to their situation.

[Image: 2604595733.png]
Quote


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Can save directly from the DataTable into the Table of Access 2000 ? dongtrien 0 611 10-31-2016 01:55 AM
Last Post: dongtrien
  Why can I save the error information into the table of SQL server ? dongtrien 0 936 09-29-2015 04:36 AM
Last Post: dongtrien



User(s) browsing this thread: 1 Guest(s)