Groups | Blog | Home
all groups > dotnet ado.net > january 2007 >

dotnet ado.net : Inserting from one db into another


RobinS
1/11/2007 10:35:21 PM
Read from one, write to the other.

Robin S.
-----------------------------------------------
[quoted text, click to view]

Denny Lim
1/11/2007 11:15:01 PM
Hi John?

no need use two connection :)

the simple :
SqlCon.Open()
SqlDataAdapter.fill(Dataset1)
SqlCon.ChangeDatabase("DestinationDatabase")
SqlDataAdapter.InsertCommand.CommandText = SQL Query + (Dataset value)
SqlDataAdapter.InsertCommand.ExecuteNonQuery

actually im not use code like that. ( it's simple for you to understands)
:)
Denny Lim
1/12/2007 12:38:02 AM
[quoted text, click to view]

of course RobinS!

'// first it's source database you want to read.
SqlCon.Open()

'// you already get the data and fill to dataset
SqlDataAdapter.fill(Dataset1)

'// Then Change database destination to database you want to write.
SqlCon.ChangeDatabase("DestinationDatabase")

'// Insert Now.
SqlDataAdapter.InsertCommand.CommandText = SQL Query + (Dataset value)
SqlDataAdapter.InsertCommand.ExecuteNonQuery

It's really works ;)
John
1/12/2007 3:36:16 AM
Hi

I have open connection to two separate databases. I now need to insert
records from a table in one db into a table in second db. How can I go about
doing it?

Thanks

Regards

William (Bill) Vaughn
1/12/2007 10:10:06 AM
If the target database is SQL Server you do NOT want to simply perform
INSERT statements unless you're being paid by the length of time it takes to
run your program.

ADO and all of the other data access interfaces are not designed to move
data from server-to-server. Use DTS, BCP or better yet ADO.NET 2.0's
SqlBulkCopy API.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

[quoted text, click to view]

John
1/12/2007 10:40:19 AM
Thanks.

Regards

[quoted text, click to view]

John
1/12/2007 11:54:42 AM
Will something like below also work;

LocalConn.Open()

Dim DBCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO Orders
SELECT * FROM [Orders] IN ''[ODBC;Driver={SQL
Server};Server=(local);Database=Northwind;Trusted_Connection=yes];'',
LocalConn)

DBCommand.ExecuteNonQuery()

LocalConn.Close()

Thanks

Regards

[quoted text, click to view]

Paul Clement
1/12/2007 12:18:10 PM
[quoted text, click to view]

¤ Will something like below also work;
¤
¤ LocalConn.Open()
¤
¤ Dim DBCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO Orders
¤ SELECT * FROM [Orders] IN ''[ODBC;Driver={SQL
¤ Server};Server=(local);Database=Northwind;Trusted_Connection=yes];'',
¤ LocalConn)
¤
¤ DBCommand.ExecuteNonQuery()
¤
¤ LocalConn.Close()

Yes it will work as long as the columns map properly in each table. Otherwise, the column names may
need to be specified in the SQL statement.


Paul
~~~~
John
1/12/2007 7:17:30 PM
Hi Bill

It is just a few records 10-20 at a time. I just need an easy way to run
insert and update queries between the two dbs.

Thanks

Regards

[quoted text, click to view]

Denny Lim
1/14/2007 6:42:00 PM
Dim DBCommand As New System.Data.OleDb.OleDbCommand("INSERT INTO Orders
[quoted text, click to view]

it's really works, but ...
assume you got a lot insert, update and delete method like above in multiple
Classes.

each query need specify 'Server' and other properties connection.

In fact you want to move connection to another server. Server=(local) ???
you need change all you code! ( OOP = is useless)

Code readability is needed.

Denny Lim
1/14/2007 6:44:00 PM
Well William ...
[quoted text, click to view]

yes you're right!
but remember!
with feature you said above need more than lot process and multiple click :)

AddThis Social Bookmark Button