Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Multiple databases with single ado.net query

I have distributive DB architecture where data is stored in multiple SQL servers.

how can i do select/update/delete by running a single query. for example "select * from employees" should return data from all databases i have.

How can write single query which run across multiple SQL servers and gets a single consolidated view to my web server.

NOTE: Since the number of SQL servers may change at varied times so I am looking for something else than linked queries since managing the linked queries at scale( up or down) is a big pain

like image 492
user72486 Avatar asked Dec 30 '09 13:12

user72486


People also ask

Can you query multiple databases?

In summary, if all your databases are on one server instance, then multiple database querying is as easy as prefixing the table name with the database or schema name depending on your database software.

Can you have multiple databases on a single server?

Yes. Multiple databases can be configured on the same server when a Concurrent User License has been purchased, as this type of license is single server.

What is cross database query?

With cross-database queries, you can seamlessly query data from any database in the cluster, regardless of which database you are connected to. Cross-database queries can eliminate data copies and simplify your data organization to support multiple business groups on the same cluster.


1 Answers

To talk to different databases / connections, you'll need a distributed transaction via TransactionScope; fortunately, this is actually easier than db-transactions (although you need a reference to System.Transactions.dll):

using(TransactionScope tran = new TransactionScope()) {
    // lots of code talking to different databases / connections
    tran.Complete();
}

Additionally, TransactionScope nest naturally, and SqlConnection enlists automatically, making it really easy to use.

like image 121
Marc Gravell Avatar answered Oct 22 '22 13:10

Marc Gravell