SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Create new Login


Create new Login

Author
Message
andrei_cantsyr
andrei_cantsyr
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 9
Hi,
I'm trying to create a new login for SQL Server 2005 using SMO in .Net C# here is the code:

ServerConnection conn = new ServerConnection();
conn.ServerInstance = @"ANDREY\SQLEXPRESS";
conn.LoginSecure = false;
conn.Login = "sa";
conn.Password = "p@ssw0rd";

Server srv = new Server(conn);

Login db_login = new Login(srv, "newuser");
db_login.LoginType = LoginType.SqlLogin;
db_login.Create("p@ssw0rd", LoginCreateOptions.None);
db_login.DefaultDatabase = "web";
db_login.Alter();

/*StringCollection sc = db_login.Script();
foreach (string s in sc)
{
Console.WriteLine(s);
}*/

db_login.AddToRole("sysadmin");

Database db1 = srv.Databases["master"];
User db_user1 = new User(db1, db_login.Name);
db_user1.UserType = UserType.SqlLogin;
db_user1.Create();

Database db2 = srv.Databases["model"];
User db_user2 = new User(db2, db_login.Name);
db_user2.UserType = UserType.SqlLogin;
db_user2.Create();

Database db3 = srv.Databases["msdb"];
User db_user3 = new User(db3, db_login.Name);
db_user3.UserType = UserType.SqlLogin;
db_user3.Create();

Database db4 = srv.Databases["web"];
User db_user4 = new User(db4, db_login.Name);
db_user4.UserType = UserType.SqlLogin;
db_user4.Create();

Database db5 = srv.Databases["tempdb"];
User db_user5 = new User(db5, db_login.Name);
db_user5.UserType = UserType.SqlLogin;
db_user5.Create();
db_user5.AddToRole("db_owner");


But it throws me the following exceptions:
Unhandled Exception: Microsoft.SqlServer.Management.Smo.FailedOperationException: Create failed for
User 'newuser'. ---> Microsoft.SqlServer.Management.Smo.PropertyNotSetException: Set property Login
to accomplish this action.
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetPropValue(String propName)
at Microsoft.SqlServer.Management.Smo.User.ScriptCreate(StringCollection createQuery, ScriptingOp
tions so)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.CreateImpl()
at Microsoft.SqlServer.Management.Smo.User.Create()
at TestApp1.Program.Main(String[] args) in C:\Documents and Settings\cantsyr\Desktop\TestApp1\Pro
gram.cs:line 81

And I'm trying to view the SQL Script and here is the script:
/* For security reasons the login is created disabled and with a random password. */
*9pE+?<↑ ?o', DEFAULT_DATABASE=[web], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POL
ICY=ON
ALTER LOGIN [newuser] DISABLE

I think that the exceptions are thrown because of security reasons, please help me to accomplish this task.
Thx
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14680 Visits: 9518
This is failing when you create the USERs, not when you are creatign the LOGIN. The USER create is failing for exactly the reason that the error message states: you have not set the .Login property of the new USER objects:

Database db1 = srv.Databases["master"];
User db_user1 = new User(db1, db_login.Name);
db_user1.UserType = UserType.SqlLogin;
db_user1.Login = db_login;
db_user1.Create();



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
andrei_cantsyr
andrei_cantsyr
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 9
thx
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14680 Visits: 9518
Glad I could help.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search