开发者

Setting sql server usermapping programmatically using C#

开发者 https://www.devze.com 2023-03-24 18:16 出处:网络
You can set the usermappings voor a user in sql server for a database, is this possible to do it in C#? for example setting db_owner permissions on a datab开发者_StackOverflowase for a user, using SMO

You can set the usermappings voor a user in sql server for a database, is this possible to do it in C#? for example setting db_owner permissions on a datab开发者_StackOverflowase for a user, using SMO.


as you said you can using SMO namespaces

have a look at the code below: it creates a user and give it the db_owner permission

static void CreateLogin
    (string sqlLoginName,string sqlLoginPassword,string databaseName)
{
myServer=GetServer()
Login newLogin = myServer.Logins[sqlLogin];
if (newLogin != null)
    newLogin.Drop();
newLogin = new Login(myServer, sqlLogin);
newLogin.PasswordPolicyEnforced = false;
newLogin.LoginType = LoginType.SqlLogin;
newLogin.Create(sqlLoginPassword);
//Create DatabaseUser
DatabaseMapping mapping = 
    new DatabaseMapping(newLogin.Name, MainDbName, newLogin.Name);
Database currentDb = myServer.Databases[databaseName];
User dbUser = new User(currentDb, newLogin.Name);
dbUser.Login = sqlLogin;
dbUser.Create();
dbUser.AddToRole("db_owner");
}

http://www.codeproject.com/KB/database/BackupRestoreWithSmo.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消