单击一个按钮的两个更新语句

时间:2013-08-19 17:10:29

标签: c# .net ado.net

我在C#窗体中使用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();

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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();
            }
        }