0

To find all database users / owners I am currently using following code

m_server is of type Microsoft.SqlServer.Management.Smo.Server:

var databases = m_server.Databases;
var result = new List<string>();

foreach (Database database in databases)
{
    if (!result.Contains(database.Owner))
    {
        result.Add(database.Owner);
    }
}

This method lacks performance.

Is there any other way to get list of users from Microsoft.SqlServer.Management.Smo.Server type variable?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 4
    Don't do this in dotnet. Do it in sql. This is sort of like using the library to tell you what text is on page 34 of a book when you could just go to the book and look at it directly. Here is a great example of doing this in t-sql. https://stackoverflow.com/questions/8471124/t-sql-to-list-all-the-user-mappings-with-database-roles-permissions-for-a-login – Sean Lange Feb 13 '18 at 21:31
  • Is the credentials of the database SQL or Windows. Usually the database is setup to use a group account in Windows. So you would need to get the users in the windows group account. – jdweng Feb 14 '18 at 00:44
  • Can you clarify what lacks performance? – tj-cappelletti Feb 14 '18 at 02:42
  • It's slow because it needs to create an instance of every database before it can get owner info. and I have a limitation for SQL queries – Mare Milojkovic Feb 19 '18 at 08:08

1 Answers1

1
      var result = new List<string>();
        foreach (Login login in m_server.Logins)
        {
            if (!login.IsDisabled)
            {
                result.Add(login.Name);
            }
        }
        return result;

I got it, this was the perfect code