Home All Groups Group Topic Archive Search About

SMO Login and User Creation Fails SQL2005

Author
3 Aug 2006 10:04 PM
JP
Hi,
   I need to create a Login and then a user for a couple of databases
programmatically. I can create the Login but not the user. IT complains about
a parent and when I try to set the parent I got another error. Below is my
code and the exact error messages. Any help is appreciated.

Private Function Create_Server_Login(ByVal Xion As ServerConnection, ByVal
The_Login As String) As String
        Dim xServer As Server = New Server(Xion)
        Dim New_Login As Login = New Login(xServer, The_Login)
        New_Login.LoginType = LoginType.SqlLogin
        New_Login.DefaultDatabase = "CIA"

        Try
            New_Login.Create("Tmp_Pwd")
            Return "0"
        Catch ex As SmoException
            Return ex.Message
        End Try
    End Function

Function Create_DB_User(The Login)
Dim db As New Database(xServer, "MyDB")
Dim xUser As New User(db, The_Login)
xUser.UserType = UserType.SqlLogin
xUser.Login = The_Login

' xUser.Parent = db ---> Fails  (2) when added
       Try
           xUser.Create()  ---> Fails  (1)
            xUser.AddToRole("Users_Role")
          Return "0"
            Catch ex As SmoException
                Return ex.Message
            End Try


(1) Cannot create User '[test4]' if parent is not yet created.
(2) SetParent failed for User 'test4
      Cannot perform the operation on this object, because the object is a
member of a collection.

SMO Creator any ideas? Perhaps I need to set some stuff up before executing
the create.

Thanks in advance

Author
3 Aug 2006 10:42 PM
Dan Sullivan
Call Create on your new database before you use it to create users, it has to actually exist before you can use it as a parent for other objects.

Dan

Dim db As New Database(xServer, "MyDB")
db.Create()
Dim xUser As New User(db, The_Login)
Author
4 Aug 2006 1:09 PM
JP
Dan,
   The database already exists. However, I tried it and it threw an error
msg "DB already exist." I'm sure I'm missing a step.

Show quote
"Dan AT Pluralsight (Dan Sullivan)" wrote:

> Call Create on your new database before you use it to create users, it has to actually exist before you can use it as a parent for other objects.
>
> Dan
>
> Dim db As New Database(xServer, "MyDB")
> db.Create()
> Dim xUser As New User(db, The_Login)
>
>
>
Author
4 Aug 2006 2:16 PM
Dan Sullivan
If the database already exists then you need to get a reference to it not create a new one. You can get a reference to a database from the Databases collection property of the Server. for example:

Dim myServer AS new Server("MyServer")
Dim db As Database
db = myServer.Databases("MyDB")
Dim xUser As New User(db, The_Login)

Dan
Author
4 Aug 2006 3:08 PM
JP
Dan,
   It worked! I replaced the first line with the second
    'Dim db As New Database(xServer, "MyDb")
     db = xServer.Databases("MyDB")

Thanks

Case Solved

Show quote
"Dan AT Pluralsight (Dan Sullivan)" wrote:

> If the database already exists then you need to get a reference to it not create a new one. You can get a reference to a database from the Databases collection property of the Server. for example:
>
> Dim myServer AS new Server("MyServer")
> Dim db As Database
> db = myServer.Databases("MyDB")
>  Dim xUser As New User(db, The_Login)
>
> Dan
>
>

AddThis Social Bookmark Button