sql insert records from one database to another database

Hello Experts,
I have 2 databases, each with 1 table.
Database_old , table_old
Database_new, table_new
Each table has name, address, email
In ASP NET, I want to be able to insert all of the records from database_new/table_new into database_old/table_old
Here is the connection string I use for Database_old
H_connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath("") & "\app_data\ Database_old.accdb;"

Do I need to open a connection string for Database_new?

What is the sql to insert all records in Database_new/table_new into Database_old/table_old ?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Yes, you would need to create a connection string for the new db as well.

Basically you need to do it in a few steps.

Open the connection string to the old DB . . . do a select * from old_table.
Execute the command as a datareader.

Build the DataReader (dr) with the following code:

StringBuilder sb = new StringBuilder();
While dr.Read()
              ("insert into new_table (name, Address,Email) Values ('{0}', '{1}', '{2}')",

Open in new window

Then open a connection the new database
Execute a command using stringBuilder.ToString() as NonExecuteQuery
Close your connections and cleanup.
williecgAuthor Commented:
Thanks for your response.

I am a newbee and what I understand is limited.
I conceptually understand your logic, but I get tripped up on the specifics of the commands.
I am using asp net vb.
Could you please provide a code example to do what you described?

Kyle AbrahamsSenior .Net DeveloperCommented:
This uses a SqlConnection, but just change your connection type / strings to be what you need:

'change the connection string
Dim conn as SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("OldDb").ConnectionString)
Dim cmdSelect as new SqlCommand = new SqlCommand("select * from old_table", conn)

'normally I'd use a try/catch . . . but this is for your example only:

Dim dr as DataReader = cmdSelect.ExecuteDataReader()

Dim sb as StringBuilder = new StringBuilder()

while (dr.Read)
              ("insert into new_table (name, Address,Email) Values ('{0}', '{1}', '{2}'); ",


'change the connection string
dim conn2 as SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings("OldDb").ConnectionString)
dim cmdInsert as SqlCommand = new SqlCommand(sb.ToString(), conn2)

Open in new window

That should do it for you.  Note I did this without a compiler so there might be some typos.  Let me know if you get any errors and/or have more issues.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
williecgAuthor Commented:
been on holiday. Thanks for the information, getting me in the right direction.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.