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 8219035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:00:53+00:00 2026-06-07T13:00:53+00:00

I have an application which can add new login to sql server and then

  • 0

I have an application which can add new login to sql server and then map it to all the DB’s and make it ‘db_owner’ (those are the requirements…).
When I built the application , I used SMO and everything worked perfect.
Now the requirements have changed , and I can’t use SMO no longer 🙁
So I need to run T-SQL commands from my application instead….
My problem is when I run the application nothing happens in the SQL server – the login is not added and it is not mapped to all the DB’s …. but when I execute those lines in the SQL server management studio it works just fine…

those are the commands –

create login dbUser WITH PASSWORD = 'passsword';

use DB_MyLog;
create user dbUser for login dbUser;
use DB_MyLog
EXEC sp_addrolemember 'db_owner','dbUser'

In my application I run them like this –

string connString = "Data Source=" + ConfigurationManager.AppSettings.Get("localhost_SQLEXPRESS")
                                    + ";Initial Catalog=" + "master" + ";User ID=" + ConfigurationManager.AppSettings.Get("user")
                                    + ";Password=" + ConfigurationManager.AppSettings.Get("password");

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connString);

        builder.AsynchronousProcessing = true;

        using (SqlConnection sqlConnection1 = new SqlConnection(builder.ConnectionString))
        {

            using (SqlCommand cmd = new SqlCommand("CREATE LOGIN " + ConfigurationManager.AppSettings.Get("dbUser") +
                                                    " WITH PASSWORD=N'" + ConfigurationManager.AppSettings.Get("dbUserPass") + "' "
                                                     , sqlConnection1))
            {

                sqlConnection1.Open();

                cmd.BeginExecuteNonQuery();

             }
        }

and in another method I run –

  string connString = "Data Source=" + ConfigurationManager.AppSettings.Get("localhost_SQLEXPRESS")
                + ";Initial Catalog=" + "master" + ";User ID=" + ConfigurationManager.AppSettings.Get("user")
                + ";Password=" + ConfigurationManager.AppSettings.Get("password");

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connString);

        builder.AsynchronousProcessing = true;

        using (SqlConnection sqlConnection1 = new SqlConnection(builder.ConnectionString))
        {
            //get all databases
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM sys.sysdatabases", sqlConnection1))
            {

                sqlConnection1.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                // Call Read before accessing data.
                while (reader.Read())
                {
                    using (SqlConnection sqlConnection2 = new SqlConnection(builder.ConnectionString))
                    {
                        sqlConnection2.Open();
                        using (SqlCommand cmd1 = new SqlCommand("use " + reader[0].ToString() + ";" +
                                                                "create user " + ConfigurationManager.AppSettings.Get("dbUser") + " for login " + ConfigurationManager.AppSettings.Get("dbUser") + ";" +
                                                                "use " + reader[0].ToString() +
                                                                " EXEC sp_addrolemember 'db_owner','" + ConfigurationManager.AppSettings.Get("dbUSer") + "'"
                                                                , sqlConnection2))
                        {
                            cmd1.BeginExecuteNonQuery();

                        }

                    }
                }

                // Call Close when done reading.
                reader.Close();

            }
        }
  • 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-06-07T13:00:54+00:00Added an answer on June 7, 2026 at 1:00 pm

    If you use BeginExecuteNonQuery and an error occurs, it only becomes visible in the callback handler. Perhaps call ExecuteNonQuery instead of BeginExecuteNonQuery.

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

Sidebar

Related Questions

Scenario: I have an application (C#) that expects a SQL database and login, which
I have an application which requires authentication, but has some related services which can
I have a console application which can also open winform under certain conditions.Processing(its a
We have a delphi application which can also run as a sevice . We
I have a socket application which I can use in local network, at home.
I have an application in which you can accept friend request. For this purpose
I have two async classes in my application which you can see below class
I have a console application from which I create a window. I can render
In my application I have groups which are essentially just tags that can be
I have a four WAR files which are modules of application. How I can

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.