2011-08-03 64 views

回答

4

如你所說,你可以使用SMO命名空間

看看下面的代碼:它創建了一個用戶,並給它的db_owner權限

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

謝謝你的工作就像一個魅力! – Bas

相關問題