Jeyhun Rahimov
Jeyhun Rahimov

Reputation: 3787

Copy data from one table in one database to another table in another database

May be there are many dublicates of this question. But I never used ADO.NET, that is why I have problem about SqlCommand with 2 connection strings.

I found code to copy data from one table in one database to another table in another database.

            SqlCommand command = new SqlCommand("SELECT * INTO Db2.dbo.Car FROM Db1.dbo.Automobile", Constr);

            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            ResultGrid.ItemsSource = dataSet.Tables["Car"].DefaultView;

I use 2 database and so, I should set 2 connecting string for every database. Where should set those?

Upvotes: 0

Views: 901

Answers (1)

Vasanth
Vasanth

Reputation: 1710

You dont need two connection string. Query you mentioned in ur code snippet need to be run on DB1 db. So your Constr just need to point to DB1 db. Your query will run on DB1 and create a new table Car in DB2 db.

Note: Assumption is that both DB1 and DB2 exists on same server and user you are using have access to create table in DB2 database.

Upvotes: 2

Related Questions