1

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();

            }
        }
2
  • 4
    If you use BeginExecuteNonQuery and an error occurs, it only becomes visible in the callback handler. Perhaps call ExecuteNonQuery instead of BeginExecuteNonQuery. Commented Jul 8, 2012 at 8:34
  • @Andomar When I changed BeginExecuteNonQuery to ExecuteNonQuery I was able to catch my exception and and to fix my error....now it works fine , thanks... If you add this suggestion to the answers I will accept it... Commented Jul 8, 2012 at 9:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Rather than re-using the SqlConnectionStringBuilder builder, does it work if you set ";Initial Catalog=" + reader[0].ToString() for sqlConnection2? You could omit the USE statements then, issue two separate SqlCommands (CREATE USER, EXEC).

Separation of these steps might help you figure out which statement fails and on which database using a try/catch inside the while() loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.