Geeks With Blogs

News
The Wrecking Bawl Destructuring query language, one keyword at a time.

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 all tech stuff , C# , SQL Server | Back to top


Comments on this post: checking for RESTORE permissions with C#/SQL

No comments posted yet.
Your comment:
 (will show your gravatar)
 


Copyright © Alex Bransky | Powered by: GeeksWithBlogs.net | Join free