General information
Title:
Why doesn't this work in code??
I am trying to create a method that determines if a user is part of the sysadmin group in order to determine if that user can create a database.
I can run the following Query in SQL Server Manager Studio and it produces multiple rows (some of which I've displayed below):
exec sp_helpsrvrolemember
sysadmin sa 0x01
sysadmin BUILTIN\Administrators 0x01020000000000052000000020020000
sysadmin NT AUTHORITY\SYSTEM 0x010100000000000512000000
But when I try to run the following code I only get one row (the first above). Any ideas?
SqlCommand cmd = new SqlCommand("sp_helpsrvrolemember", MyConnection);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
string Role;
string MemberName;
while (rd.Read())
{
Role = (string)rd["ServerRole"];
Role = Role.Trim().ToLower();
MemberName = (string)rd["MemberName"];
MemberName.Trim().ToLower();
if (Role == "sysadmin" && MemberName.Contains(username))
return true;
}
Thanks in advance.
Bob
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only