Tuesday, May 3, 2011

How do I find the owner of a given database?

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.

From stackoverflow
  • 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

    Esteban Araya : Would you happen to have a query that works against Oracle, MySQL, etc.?
    marc_s : 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.
  • 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.

0 comments:

Post a Comment