Home All Groups Group Topic Archive Search About

Inserting from one db into another

Author
12 Jan 2007 3:36 AM
John
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

Author
12 Jan 2007 6:35 AM
RobinS
Read from one, write to the other.

Robin S.
-----------------------------------------------
Show quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:ugfmMrfNHHA.5012@TK2MSFTNGP02.phx.gbl...
> 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
>
>
Author
12 Jan 2007 8:38 AM
Denny Lim
"RobinS" wrote:

> Read from one, write to the other.
>

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 ;)
Author
11 May 2007 9:09 PM
Jim
Denny Lim,

If the table in the Source Database field names are FieldA and FieldB
and the "DestinationDatabase" has fields are Field1 and Field2, what
would the SQL Query + (Dataset value) be in your example below?

Jim

Denny Lim wrote:
Show quote
> "RobinS" wrote:
>
>> Read from one, write to the other.
>>
>
> 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 wrote:
> 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
Author
12 Jan 2007 7:15 AM
Denny Lim
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)
Show quote
:)
Author
12 Jan 2007 10:40 AM
John
Thanks.

Regards

Show quote
"Denny Lim" <Denny***@discussions.microsoft.com> wrote in message
news:8F2C6E3B-A0BB-45A3-A7BE-F6B5BEB0319D@microsoft.com...
> 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)
> :)
>
Author
12 Jan 2007 11:54 AM
John
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

Show quote
"Denny Lim" <Denny***@discussions.microsoft.com> wrote in message
news:8F2C6E3B-A0BB-45A3-A7BE-F6B5BEB0319D@microsoft.com...
> 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)
> :)
>
Author
12 Jan 2007 6:18 PM
Paul Clement
On Fri, 12 Jan 2007 11:54:42 -0000, "John" <John@nospam.infovis.co.uk> wrote:

¤ 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
~~~~
Microsoft MVP (Visual Basic)
Author
15 Jan 2007 2:42 AM
Denny Lim
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)

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.

hope this can help too ;)
Author
12 Jan 2007 6:10 PM
William (Bill) Vaughn
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)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:ugfmMrfNHHA.5012@TK2MSFTNGP02.phx.gbl...
> 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
>
>
Author
12 Jan 2007 7:17 PM
John
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

Show quote
"William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> wrote in message
news:OyBIjTnNHHA.4604@TK2MSFTNGP06.phx.gbl...
> 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)
> -----------------------------------------------------------------------------------------------------------------------
>
> "John" <John@nospam.infovis.co.uk> wrote in message
> news:ugfmMrfNHHA.5012@TK2MSFTNGP02.phx.gbl...
>> 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
>>
>>
>
>
Author
15 Jan 2007 2:44 AM
Denny Lim
Well William ...
> 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.

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

with Executeable Program at least move that record just with 3 mouse clicks :)

AddThis Social Bookmark Button