Is there a method for checking which roles a user has in SQL using SMO?
I can add a role via:

user.AddToRole("Administrators")

But cannot see an obvious method to progmatically list them.

Recommended Answers

All 2 Replies

Yes, there is EnumRoles function which returns a string collection.

Here's an example

For Each RoleName As String In user.EnumRoles()
  Console.WriteLine(RoleName)
Next

HTH

Thanks, that explains it.
This is what I'm trying to do (add the user to a role if it's not a member:

For Each RoleName As String In user.EnumRoles()
              If RoleName <> "Administrator" Then
                user.AddToRole("Administrator")
              End If
             Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.