Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:03:33+00:00 2026-05-24T09:03:33+00:00

I am automating login creations on a SQL Server 2008 database using SMO in

  • 0

I am automating login creations on a SQL Server 2008 database using SMO in VB.NET. I have created the login, created the user in the database, given the user roles, and set the login name of the user to the login I created at the server level.

What I haven’t been able to do is get the User Mapping right. But maybe I am misunderstanding something. I am not a SQL Security guru by any means.

Sub Main()
    Dim TargetServerName As String = "MyServer"
    Dim TargetDBName As String = "PermissionTestDB"
    Dim strGroupName As String = "TestGroup"
    Dim TargetServer As New Server(TargetServerName)
    Dim TargetDataBase As Database = TargetServer.Databases(TargetDBName)

    ' First we will create a SQL Server Login
    Dim newLogin As New Login(TargetServer, String.Format("DOMAIN\{0}", strGroupName))
    newLogin.LoginType = LoginType.WindowsGroup
    newLogin.Create()

    'Next we will create a Database User login
    Dim dbLogin As New User(TargetDataBase, newLogin.Name)
    TargetDataBase.Users.Add(dbLogin)
    dbLogin.Login = newLogin.Name
    newLogin.DefaultDatabase = TargetDBName

    'Assign the Database Login some roles
    Dim DataReaderServerRole As DatabaseRole = TargetDataBase.Roles("db_datareader")
    Dim DataWriterServerRole As DatabaseRole = TargetDataBase.Roles("db_datawriter")
    DataReaderServerRole.AddMember(dbLogin.Name)
    DataWriterServerRole.AddMember(dbLogin.Name)

    'And Add a permission under the dbo schema
    Dim dboPermission As New ObjectPermissionSet(ObjectPermission.Alter)
    TargetDataBase.Schemas("dbo").Grant(dboPermission, newLogin.Name)

    'Map the Database User to the Server login
    Dim dbMapping As New DatabaseMapping(newLogin.Name, TargetDBName, dbLogin.Name)
End Sub

The last line creates a mapping, and in another sub I called the EnumDatabaseMappings method and the mapping is there, however when I go into Management Studio and go to SERVER>Security>Logins>”Domain\TestGroup”>User Mappings page, the line in the list for PermissionTestDB is not checked. In the grid below, I can see that the login and associated user have the db_datareader and db_datawriter roles enabled, but it is all grayed out.

Am I going about this the wrong way? DatabaseMapping class doesn’t have very extensive documentation, so any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:03:33+00:00Added an answer on May 24, 2026 at 9:03 am
    sp_revokedbaccess 'username'
    sp_adduser 'username'
    sp_addrolemember 'role', 'username'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are automating Excel using VB.Net, and trying to place multiple lines of text
I'm automating some source control software functionality using a dot bat script but given
We have an intra-net website that requires me to enter login information every time
I'm automating form login in a certain site using the WebBrowser control. Having the
I'm using sessions in Django to store login user information as well as some
I have created a small app in C using WinAPI and I have a
Recently I have developed an automation application using WatiN API, in .NET Framework 3.5,
i have an asp.net-mvc site with sqlserver backend and i am using membershipprovider for
I'm a Java developer and I have a question about automating a task I've
There are some HTML based games (ie bootleggers.us) that have a simple login form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.