Solved

Quickest way to insert records from one db into another

Posted on 2006-10-20
10
210 Views
Last Modified: 2010-04-16
Hi,

I have a DataTable entitled "dtSource" from database 'A'.  I would like to push the data into database 'B'.  Both databases are SQL Server 2005 and both table schemas are the same.

I suppose I could loop through the rows like so:

string sql = "";
foreach(DataRow dr in dt.Rows)
{
    sql += "INSERT INTO...."
    sql += dr[0].ToString() + ", ";
    etc....
}

There has to be a faster and/or more of an elegant way of doing this.

Thanks
0
Comment
Question by:brdrok
  • 4
  • 3
  • 3
10 Comments
 
LVL 12

Accepted Solution

by:
topdog770 earned 300 total points
ID: 17775093

foreach(DataRow dr in dt.Rows)
{
    myDestinationDataTable.ImportRow(dr);
}

Then you can use Update to populate the second database



0
 
LVL 12

Expert Comment

by:topdog770
ID: 17775098
Let me know if you would like a bit more detail or if this is just the nudge you were looking for
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17775369
Just execute the following SQL

sql = "Insert INTO dbo.A.dtSource " +
     "SELECT * FROM dbo.b.dtSource";
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 28

Expert Comment

by:strickdd
ID: 17775373
This is quicker and cleaner because SQL can process it faster than ASP.Net code can loop through a DataTable.
0
 
LVL 7

Author Comment

by:brdrok
ID: 17775428
Thank you for this informational gold nugget...

I feel kind of silly asking this but I have a table with 40+ columns, is there anyway to just call the Update() without having to specify a gazillion parameters like the following?

// Create the InsertCommand.
   command = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    // Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

thanks
0
 
LVL 7

Author Comment

by:brdrok
ID: 17775445
// Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, dr["CustomerID"]);
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, dr["CompanyName"]);

etc
0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 200 total points
ID: 17775603
If you are JUST inserting information the code I gave you will not require ANY parameters. If you JUST need to update then i believe this format will work:

UPDATE <table_name> <alias>
SET (<column_name>,<column_name> ) = (
   SELECT (<column_name>, <column_name>)
   FROM <table_name>
   WHERE <alias.column_name> = <alias.column_name>);


if you need to do both, you will need to find out which rows you are inserting and the run an insert on those rows after the update.
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17775685
In Oracle, you would need to have a database link enabled to copy data from one database to another.
If you can copy from db to db directly in MS SQL then strickdd's approach is a good one


In this approach
===================
foreach(DataRow dr in dt.Rows)
{
    myDestinationDataTable.ImportRow(dr);
}
====================

1. Use an OleDataAdapter to fill the source table
2. Use an OleDataAdapter to set the schema in the destination table
3. Populate the destination table by using the import row approach
4. call the OleDataAdapter's Update for the destination table and you should be set.
You will not have to specifiy any parameters at all.



0
 
LVL 28

Expert Comment

by:strickdd
ID: 17775878
topdog770,
    If you read the initial question it is clearly stated that "Both databases are SQL Server 2005".
0
 
LVL 7

Author Comment

by:brdrok
ID: 17776695
Gentleman...thank you very much.  Learned quite a bit today.  

The only thing I had to add is the SetAdded() method

foreach(DataRow dr in dt.Rows)
{
    dr.SetAdded();
    myDestinationDataTable.ImportRow(dr);
}

otherwise my Update() wouldn't be able to find any records to insert.

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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