none
SMO Login Permissions RRS feed

  • Question

  • Hey there,

    I'm developing a piece of code which manipulates SQL Server databases. The code uses SMO objects and performs loads of actions on the SQL Server. Therefore I want to check the user's permissions before starting all these actions. How can I use SMO to check if the current logged in user has the sysadmin server role?

    Thanks a lot!

    Eduard 

    Thursday, February 10, 2011 1:41 PM

Answers

  • Hmmm, It took me a while... After I became frustrated I posted the question and ofcourse half an hour later found out how it works. So for those who need the answer :

    private bool CheckManagementPermission()
    {
    	bool hasManagementPermission = false;
    	Server dbServer = new Server(m_Connection);
    	if (m_Connection.IsOpen)
    	{
    		// User needs to be system admin
    		if (m_Connection.IsInFixedServerRole(FixedServerRoles.SysAdmin))
    		{
    			hasManagementPermission = true;
    		}
    	}
    	return hasManagementPermission;
    }
    

    • Marked as answer by Nikneem Thursday, February 10, 2011 2:31 PM
    Thursday, February 10, 2011 2:30 PM

All replies

  • Hmmm, It took me a while... After I became frustrated I posted the question and ofcourse half an hour later found out how it works. So for those who need the answer :

    private bool CheckManagementPermission()
    {
    	bool hasManagementPermission = false;
    	Server dbServer = new Server(m_Connection);
    	if (m_Connection.IsOpen)
    	{
    		// User needs to be system admin
    		if (m_Connection.IsInFixedServerRole(FixedServerRoles.SysAdmin))
    		{
    			hasManagementPermission = true;
    		}
    	}
    	return hasManagementPermission;
    }
    

    • Marked as answer by Nikneem Thursday, February 10, 2011 2:31 PM
    Thursday, February 10, 2011 2:30 PM
  • Hi Nikneem,

     I’m glad to hear that you got it working. Thank you for sharing your experience here.

    It will be very beneficial for other community members having the similar questions.

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 11, 2011 1:20 AM
    Moderator
  • So Nikneem Are you saying that a user needs to have SA rights to use sMO objects?
    Edward R. Joell MCSD MCDBA
    Tuesday, March 1, 2011 1:15 AM