1

我有一个应用程序可以向 sql server 添加新登录名,然后将其映射到所有数据库并使其成为“db_owner”(这些是要求......)。当我构建应用程序时,我使用了 SMO,一切都很完美。现在要求已经改变,我不能再使用 SMO 了没有添加登录名,也没有映射到所有数据库......但是当我在 SQL Server 管理工作室中执行这些行时,它工作得很好......

这些是命令-

create login dbUser WITH PASSWORD = 'passsword';

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

在我的应用程序中,我像这样运行它们 -

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

             }
        }

在另一种方法中我运行 -

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

            }
        }
4

2 回答 2

1

如果您为 sqlConnection2 设置 ";Initial Catalog=" + reader[0].ToString() ,而不是重新使用 SqlConnectionStringBuilder 构建器,它是否有效?然后,您可以省略 USE 语句,发出两个单独的 SqlCommand(CREATE USER、EXEC)。

将这些步骤分开可能会帮助您确定哪个语句失败以及在哪个数据库上使用 while() 循环中的 try/catch。

于 2012-07-08T09:01:30.937 回答
1

如果您使用BeginExecuteNonQuery并发生错误,则它仅在回调处理程序中可见。也许调用ExecuteNonQuery而不是BeginExecuteNonQuery.

于 2012-07-08T09:23:19.350 回答