如何正确执行以下更新:
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,sql4); using (OracleConnection conn = new OracleConnection()) using (OracleCommand cmdUpdate = new OracleCommand(sql,conn)) { conn.Open(); recs = cmdUpdate.ExecuteNonQuery(); }