0

我在 C# windows 窗体中使用 SQL 数据适配器,我试图在两个单独的数据库上运行两个更新语句,只需单击一个按钮,使用相同的字符串。我不断收到异常错误,我认为第二个更新语句正在尝试将连接用于第一个更新语句,但我无法弄清楚。有人有什么想法吗?

                string resendacct = textBox_resendVisit.Text.Trim();

                SqlConnection CONN1 = new SqlConnection("Data Source=DBSERVER1.domain.company.org; Initial Catalog=db1; User ID=username; Password=password    ;Trusted_Connection=False");

                //OPEN SQL CONNECTION
                CONN1.Open();

                //CREATE NEW SQLCOMMAND
                SqlCommand cmd = CONN1.CreateCommand();
                cmd.CommandText =
                 "UPDATE PRODUCTIONDB1.dbo.date_time  " +
                     "SET PRODUCTIONDB1.dbo.date_time.sent_datetime = null  " +
                 "FROM PRODUCTIONDB1.dbo.date_time  " +
                 "INNER JOIN PRODUCTIONDB1.dbo.useridentity  " +
                 "ON PRODUCTIONDB1.dbo.useridentity.custidentity = PRODUCTIONDB1.dbo.date_time.custidentity  " +
                 "WHERE PRODUCTIONDB1.dbo.useridentity.account = @resendtimes";
                cmd.Parameters.AddWithValue("@resendtimes", resendacct);
                cmd.ExecuteNonQuery();

                int noRows;

                noRows = cmd.ExecuteNonQuery();


                //THIS COMMAND RESENDS THE DATABASE2 REPORTS!//

                SqlConnection CONN2 = new SqlConnection("Data Source=DBSERV2.domain.company.org; Initial Catalog=db2; Integrated Security=TRUE");

                //OPEN SQL CONNECTION
                CONN2.Open();

                //CREATE NEW SQL COMMAND
                SqlCommand cmd1 = CONN2.CreateCommand();
                cmd.CommandText =
                    "UPDATE PRODUCTIONDB2.dbo.date_time  " +
                        "SET PRODUCTIONDB2.dbo.date_time.timedone = null  " +
                    "FROM PRODUCTIONDB2.dbo.date_time  " +
                    "INNER JOIN PRODUCTIONDB2.dbo.custidentity  " +
                    "ON PRODUCTIONDB2.dbo.custidentity.id = PRODUCTIONDB2.dbo.date_time.custencounter_id  " +
                    "INNER JOIN PRODUCTIONDB2.dbo.custencounter  " +
                    "ON PRODUCTIONDB2.dbo.custencounter.id = PRODUCTIONDB2.dbo.custidentity.custencounter_id  " +
                    "WHERE PRODUCTIONDB2.dbo.custencounter.accountnumber = @resendtimes";
                cmd.Parameters.AddWithValue("@resendtimes", resendacct);
                cmd.ExecuteNonQuery();

                int noRows1;

                noRows1 = cmd1.ExecuteNonQuery();
                //CLOSE SQLCONNECTIONS
                CONN1.Close();
                CONN2.Close();
4

2 回答 2

1

您的第二个命令被分配给您创建的第一个命令变量。该命令正在使用第一个连接。检查你的 cmd 和 cmd1。我认为您的第二条sql语句应该分配给cmd1而不是cmd。

于 2013-08-19T17:17:44.503 回答
1

您可以在下面阅读您的答案。但在此之前我必须说你应该像你的连接一样

using(SqlConnection sqlConn = new SqlConnection())
{
   //your code here.
}

在您的第二个命令中,您正在对您执行所有操作,cmd而它们应该在cmd1。你应该这样编码。

using (SqlConnection CONN1 = new SqlConnection("Data Source=DBSERVER1.domain.company.org; Initial Catalog=db1; User ID=username; Password=password    ;Trusted_Connection=False"))
        {
            //OPEN SQL CONNECTION
            CONN1.Open();

            //CREATE NEW SQLCOMMAND
            using (SqlCommand cmd = CONN1.CreateCommand())
            {
                cmd.CommandText =
                 "UPDATE PRODUCTIONDB1.dbo.date_time  " +
                     "SET PRODUCTIONDB1.dbo.date_time.sent_datetime = null  " +
                 "FROM PRODUCTIONDB1.dbo.date_time  " +
                 "INNER JOIN PRODUCTIONDB1.dbo.useridentity  " +
                 "ON PRODUCTIONDB1.dbo.useridentity.custidentity = PRODUCTIONDB1.dbo.date_time.custidentity  " +
                 "WHERE PRODUCTIONDB1.dbo.useridentity.account = @resendtimes";
                cmd.Parameters.AddWithValue("@resendtimes", resendacct);
                cmd.ExecuteNonQuery();

                int noRows;

                noRows = cmd.ExecuteNonQuery();
            }
        }

        //THIS COMMAND RESENDS THE DATABASE2 REPORTS!//

        using (SqlConnection CONN2 = new SqlConnection("Data Source=DBSERV2.domain.company.org; Initial Catalog=db2; Integrated Security=TRUE"))
        {
            //OPEN SQL CONNECTION
            CONN2.Open();

            //CREATE NEW SQL COMMAND
            using (SqlCommand cmd1 = CONN2.CreateCommand())
            {
                cmd1.CommandText =
                    "UPDATE PRODUCTIONDB2.dbo.date_time  " +
                        "SET PRODUCTIONDB2.dbo.date_time.timedone = null  " +
                    "FROM PRODUCTIONDB2.dbo.date_time  " +
                    "INNER JOIN PRODUCTIONDB2.dbo.custidentity  " +
                    "ON PRODUCTIONDB2.dbo.custidentity.id = PRODUCTIONDB2.dbo.date_time.custencounter_id  " +
                    "INNER JOIN PRODUCTIONDB2.dbo.custencounter  " +
                    "ON PRODUCTIONDB2.dbo.custencounter.id = PRODUCTIONDB2.dbo.custidentity.custencounter_id  " +
                    "WHERE PRODUCTIONDB2.dbo.custencounter.accountnumber = @resendtimes";
                cmd1.Parameters.AddWithValue("@resendtimes", resendacct);
                cmd1.ExecuteNonQuery();

                int noRows1;

                noRows1 = cmd1.ExecuteNonQuery();
            }
        }
于 2013-08-19T17:19:17.663 回答