问题描述
1)如果已经有一个登录用户'user1'然后,我通过调用 CreateDatabaseUser
函数
$ b
2)否则,我创建一个登录user1的密码密码1使用默认的数据库为DB1(仅在使用创建DB1)后 CreateServerLogin
然后我用 CreateDatabaseUser
方法(2)抛出'DB1'创建用户USER1在
异常 newUser.Create();
在 CreateDatabaseUser
说, Microsoft.SqlServer.Management .Smo.FailedOperationException:创建失败,用户USER1。 ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException:执行Transact-SQL语句或批处理时出现异常。 ---> System.Data.SqlClient.SqlException:'[USER1]不是一个有效的登录,或者您没有权限。
这可能是什么问题?
私有静态登录CreateServerLogin(Server服务器,字符串数据库,字符串登录,字符串密码)
{
变种newLogin =新的登录(服务器,登录)
{
LoginType = LoginType.SqlLogin,
DefaultDatabase =数据库,
PasswordPolicyEnforced = FALSE
};
newLogin.Create(密码);
返回newLogin;
}
私有静态用户CreateDatabaseUser(数据库数据库,字符串用户,串登录)
{
变种NEWUSER =新用户(数据库,用户){用户类型=用户类型.SqlLogin,登录=登录};
newUser.Create();
newUser.AddToRole(的db_owner);
返回NEWUSER;
}
清理交易和批次。
DDL语句也支持事务,所以当你创建新的用户,并通过服务器登录,可能不存在这样的登录的服务器上,因为此前的说法是不承诺。
还有一批亲密声明GO将有助于
请先尝试:
-
CreateServerLogin
-
问题GO
-
CreateDBLogin
-
问题COMMIT
如果失败尝试这样的逻辑:
-
CreateServerLogin
-
问题COMMIT
-
CreateDBLogin
1) If there is already a login 'user1' then I create a user 'user1' for database 'db1' by calling CreateDatabaseUser
function
2) Else, I create a login 'user1' with password 'password1' with default database as 'db1' (only after creating db1) using CreateServerLogin
and then I create user 'user1' for 'db1' using CreateDatabaseUser
Method (2) throws an exception at newUser.Create();
in CreateDatabaseUser
saying, Microsoft.SqlServer.Management.Smo.FailedOperationException: Create failed for User 'user1'. ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. --- > System.Data.SqlClient.SqlException: '[user1]' is not a valid login or you do not have permission.
What could be the problem?
private static Login CreateServerLogin(Server server, string database, string login, string password)
{
var newLogin = new Login(server, login)
{
LoginType = LoginType.SqlLogin,
DefaultDatabase = database,
PasswordPolicyEnforced = false
};
newLogin.Create(password);
return newLogin;
}
private static User CreateDatabaseUser(Database database, string user, string login)
{
var newUser = new User(database, user) { UserType = UserType.SqlLogin, Login = login };
newUser.Create();
newUser.AddToRole("db_owner");
return newUser;
}
check up transactions and batches.
DDL statements also support transactions, so when you create new user and pass server login, there might not be such login on the server because previous statement being not committed.
Also batch close statement "GO" would help
Try first:
CreateServerLogin
issue GO
CreateDBLogin
issue COMMIT
if failed try this logic:
CreateServerLogin
issue COMMIT
CreateDBLogin
这篇关于问题在使用SMO中的SqlServer创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!