Posts
120
Comments
125
Trackbacks
22
checking for RESTORE permissions with C#/SQL

In case anyone's interested, here's a line of C# that lets you check to see whether the selected user (Windows or SQL) has RESTORE permissions on a specific database.  It uses SMO, and you'll need to use System.Security.Principal to get the Windows user logged on to your application, if not using SQL authentication.  Pull out the stuff in quotes if you want to try it in Query Analyzer.  This page is helpful in understanding the RESTORE command and who should have permissions to run it.

Boolean canRestore = Convert.ToBoolean(srvr.ConnectionContext.ExecuteScalar("IF (IS_SRVROLEMEMBER ('sysadmin') = 1 ) BEGIN SELECT 1 END ELSE IF (IS_SRVROLEMEMBER ('dbcreator') = 1 ) BEGIN SELECT 1 END ELSE IF ( (SELECT COUNT(*) FROM (SELECT [name],SUSER_SNAME(sid) AS [user] FROM master..sysdatabases WHERE [name] = '" + [dbName] + "') t1 WHERE [user] = '" + [selectedUser] + "') > 0 ) BEGIN SELECT 1 END ELSE BEGIN SELECT 0 END"));

posted on Wednesday, January 10, 2007 8:20 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
 
News