0

Am looking to do a sql query which uses 2 databases. How can I use 2 connection strings for this query as it is 2 different databases?

var commandText = @"insert into database1 select * from database2";

        using (SqlConnection myConn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand myCommand = new SqlCommand(commandText, myConn))
            {
                myConn.Open();
                myCommand.ExecuteNonQuery();
            } 
6
  • 1
    I would recommend creating a Linked Server if possible. Commented Feb 20, 2013 at 14:24
  • 1
    Generally not possible. If these databases reside on same MS SQL server, then you can/should use single connection string and fully qualified table names. Moreover, you can't select * from database - you can only select * from table. Commented Feb 20, 2013 at 14:25
  • Are the databases on the same server? Commented Feb 20, 2013 at 14:26
  • 1
    the SQL you are showing is talking about tables, not databases... Commented Feb 20, 2013 at 14:27
  • If Both database in same server then try like this : insert into [database1].[dbo].[TableName] select * from [database2].[dbo].[TableName] Commented Feb 20, 2013 at 14:29

4 Answers 4

2

If your both data bases in the same server instance then you don't need two connections. You are virtually connecting to the server instance. So there is no problem.

var commandText = @"insert into [dbname1].dbo.table1 select * from 
                   [dbname2].dbo.table1";

I think best way is to create a stored procedure and pass dbnames (from your configurations) to the sp as a parameters. Then, build a dynamic query to perform the insert. That way you have a better control over different database names.

If they are in two different instances or separate servers, then use sp_addlinkedserver to link them and use sp_addlinkedsrvlogin to login. Also make sure to drop them after performing the action.

As a general advice, this is not recommended as its error prone. Bit more details on comments...

Sign up to request clarification or add additional context in comments.

3 Comments

@MarcGravell, can you elaborate?
most I've worked with want databases to be silos; if you need to talk to 2 : fine; but talk to the 2 separately, so that they have liberty to move databases around without as much impact. Also: there are scenarios where each database would warrant separate accounts, etc
@MarcGravell, I agree, it could be an advice. Also if the db names are changed this will fail.
1

You can't. However, if you have control over the databases, you can set them up for a cross-server select using a single connection string.

Comments

0

I think you can't. You should execute the select statement on the first database and then the insert into the second database.

Comments

0

Create a login that has access to both databases and just use a single connection string. Your query will be like:

string sql = "INSERT INTO [Database1].[dbo].[Table] SELECT * FROM  [Database2].[dbo].[Table]

1 Comment

[Database1].dbo.[Table] (twice), surely... but I should emphasize for the OP - this is not good practice and most DBAs will shoot you if they catch you doing this...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.