从.net运行T-SQL命令

时间:2012-07-08 08:27:23

标签: .net sql sql-server-2008 tsql c#-2.0

我有一个应用程序可以向sql server添加新的登录,然后将其映射到所有数据库并将其设为'db_owner'(这些是要求......)。 当我构建应用程序时,我使用SMO,一切都很完美。 现在要求已经改变了,我不能再使用SMO了:( 所以我需要从我的应用程序运行T-SQL命令.... 我的问题是当我运行应用程序时没有在SQL服务器中发生任何事情 - 登录没有添加,它没有映射到所有的数据库....但是当我在SQL服务器管理工​​作室中执行这些行时,它工作得很好。 ..

这些是命令 -

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

            }
        }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

如果使用BeginExecuteNonQuery并发生错误,则只会在回调处理程序中显示。也许请拨打ExecuteNonQuery而不是BeginExecuteNonQuery