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