tags:

views:

110

answers:

2

Using .NET's DbConnection.GetSchema(), how do I find the owner of a given database?

Alternatively, if you have another solution that is not coupled to a specific impelementation of SQL, I'd like to hear that as well.

+3  A: 

The GetSchema call of DbConnection unfortunately doesn't retrieve the DB owner for you :-(

But you can try this on SQL Server:

select 
    db.name, db.database_id, l.name, l.type 
from 
    sys.databases db
inner join
    sys.login_token l on db.owner_sid = l.sid

If you want to connect to SQL Server from .NET, you could use the SMO (SQL Management Objects) and find your owner like this:

    Server server = new Server("Your Server");

    Database db = server.Databases["Your Database"];

    Console.WriteLine("Database owner is: " + db.Owner);

Marc

marc_s
Would you happen to have a query that works against Oracle, MySQL, etc.?
Esteban Araya
No, sorry, I'm not familiar with those database systems. And unfortunately, there really doesn't seem to be a universal database-agnostic catalog view (INFORMATION_SCHEMA) that shows the owner.
marc_s
+1  A: 

I don't believe that the SQL-92 standard specifies that a Catalog (a database) must have an owner. As such, I don't know that you can get a non-implementation-specific way of doing this.

Tom H.