IMO will best use a different connection string to achieve multi-user protection against similar databases. Ideally, ignore the code so that most of your code does not need it, but just does:
using(var conn = Somewhere.GetOpenConnection()) {
or worst case scenario:
using(var conn = Somewhere.GetOpenConnection(Environment.Published)) {
(here Environment is enum that various databases represent)
where GetOpenConnection determines which database is needed and either searches for the construction of the correct connection string.
But specifically:
- you cannot parameterize the database name in the query
- using
use between operations would be a very bad idea in terms of reusing a connection - you can explicitly use three-part identifiers (i.e.
DB1..SomeTable or DB1.dbo.SomeTable ), but this naturally does not scale for a large number of databases.
source share