0

I am trying to add new roles to a SQL 2005 database via the SMO assemblies. The Roles.Add method just does not seem to add the new role. I have my user account set as securityadmin and sysadmin.

Below is the code extract that I am trying to use to set the new role: [assuming d has been set to a database object]

        Dim dr As New DatabaseRole
        dr.Name = r
        dr.Parent = d
        dr.Owner = d.Name
        d.Roles.Add(dr)

        'Error here "<role name = r> does not exist in the current database."'
        dr.AddMember("dbo")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It's not really an answer, but you can create roles without DMO. SSMS allows you to add a role and then generate an SQL script for it. You can fire that script from code, instead of using DMO. – Andomar Nov 07 '09 at 13:18

1 Answers1

1

You need to first create the role and then assign it to the database.

Change your code to:

Dim dr As New DatabaseRole        
dr.Name = r        
dr.Parent = d        
dr.Owner = d.Name  
dr.Create();      
d.Roles.Add(dr)        
dr.AddMember("dbo")
Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111