C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

Copy data from one table to another using SqlBulkCopy class:


Posted Date: 22 Nov 2008    Resource Type: Code Snippets    Category: ADO.NET
Author: shekharMember Level: Gold    
Rating: 1 out of 5Points: 7



The primary purpose of the SqlBulkCopy class is to copy data from one source to other.
Example :

string connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);

SqlCommand myCommand = new SqlCommand("SELECT * FROM Person", myConnection);

myConnection.Open();

SqlDataReader dr = myCommand.ExecuteReader();

SqlConnection myNewConnection = new SqlConnection(connectionString);

myNewConnection.Open();

SqlBulkCopy bulk = new SqlBulkCopy(myNewConnection);

bulk.DestinationTableName = "[Person3]";

try

{

bulk.WriteToServer(dr);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

finally

{

myNewConnection.Close();

dr.Close();

myConnection.Close();

bulk.Close();

}






Responses

Author: Gaurav Arora    23 Nov 2008Member Level: Diamond   Points : 1
Hi Shekhar,

This is a good and workable code. I like the way of writting.

Keep the spirit.

Best of luck!

Thanks & regards,
Gaurav Arora


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Copy data from one table to another using SqlBulkCopy class:  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Dataset-Dataview-ArrayList
Previous Resource: Sample code to delete the data from the data base
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use