如何正确进行以下更新:
using (OracleConnection conn = new OracleConnection())
using (selCmd)
{
string sql1 = "update Table1 set name = joe where id = 10;"
string sql2 = "update Table2 set country = usa where region = americas;"
string sql3 = "update Table3 set weather = sunny where state = CA;"
string sql4 = "update Table4 set engine = v8 where maker = benz;"
cmdUpdate.CommandText = sql(#);
cmdUpdate.Connection = conn;
recs = cmdUpdate.ExecuteNonQuery();
}
如果它是一个事务,我会全知或一无所知,但我只是想看看它是如何以正确的方式运作的。
我正在考虑迭代项目数组 [sql1,sql2,sql3,sql4] 并在 CommandText 中传递 sql(#) 并每次执行 ExecuteNonQuery。
最佳答案
如果我没记错的话,可以将多个 SQL 语句连接到一个字符串中,用分号 (;) 分隔。否则,执行多个 ExecuteNonQuery()
调用没有任何问题。
string sql1 = "BEGIN update Table1 set name = 'joe' where id = 10;",
sql2 = "update Table2 set country = 'usa' where region = 'americas';",
sql3 = "update Table3 set weather = 'sunny' where state = 'CA';",
sql4 = "update Table4 set engine = 'v8' where maker = 'benz'; END;";
string sql = string.Format("{0}{1}{2}{3}",sql1,sql2,sql3,sql4);
using (OracleConnection conn = new OracleConnection())
using (OracleCommand cmdUpdate = new OracleCommand(sql, conn))
{
conn.Open();
recs = cmdUpdate.ExecuteNonQuery();
}
关于c# - 在一个连接中执行多个 UPDATE 语句的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595673/