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

  • Home
  • SEARCH
  • 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 1064895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:44:01+00:00 2026-05-16T19:44:01+00:00

in our company’s product, we attach a database to SQL Server, and we need

  • 0

in our company’s product, we attach a database to SQL Server, and we need to add credentials to some accounts to the catalog.

Currently we’re using SMO to do this, but this gives us problems (x86 vs x64, SQL Server 2005 vs 2008, etc)

Is there’s anyway of adding a login (to the server) and a user (to the catalog) using SQL?

This is the current code to add login:

connection = new ServerConnection(new SqlConnection(this.connectionString));
Server server = new Server(connection);

//Drop existing login (if exist)
Login login = server.Logins[AccountName];
if (login != null)
    login.Drop();

//Create new login
login = new Login(server, AccountName);
login.LoginType = LoginType.WindowsUser;

login.Create();
DatabaseMapping mapping = new DatabaseMapping(login.Name, connection.DatabaseName, login.Name);

and to add user:

connection = new ServerConnection(new SqlConnection(this.connectionString));
Server server = new Server(connection);
Microsoft.SqlServer.Management.Smo.Database database = server.Databases[CatalogName];
User user = server.Databases[CatalogName].Users[LoginName];
if (user != null)
    user.Drop();

user = new User(database, LoginName);
user.Login = LoginName;
user.Create();
user.AddToRole("db_owner");

I want to perform these operations using SQL. Any ideas?

  • 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-16T19:44:01+00:00Added an answer on May 16, 2026 at 7:44 pm

    CREATE LOGIN and CREATE USER

    To test for a login first:

    IF SUSER_ID('Login') IS NULL
    

    To test for a user first

    IF DATABASE_PRINCIPAL_ID('User') IS NULL
    

    To add user to role:

    EXEC sp_addrolemember 'Role', 'User'
    

    FYI, SMO replicates SQL functionality. I don’t think there is anything that can be done only SMO (there is plenty of SQL only stuff of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our company currently has seven dedicated SQL Server 2008 servers each running an average
Our company has a product which relies on local database to work (it allows
Our company have a need to set up some solution, that would allow us
Our company is considering upgrading our SQL server. At this point, would it be
Our company has a Batch Application which runs every day, It does some database
Our company has a share point document server where the UNC looks something like
Our company, Company A, may soon be partnering with Company B under some sort
Our company is offering some trainings and asks us what would we want to
Our company publishes our software product's documentation using a custom-built content management system using
Our company deals with various online contests. As a part of functionality we need

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.