Mare Milojkovic
Mare Milojkovic

Reputation: 77

Find all SQL Server database users in C#

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?

Upvotes: 0

Views: 2030

Answers (1)

Mare Milojkovic
Mare Milojkovic

Reputation: 77

      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

Upvotes: 1

Related Questions