如何在一个查询中传递多个命令?

时间:2014-06-23 09:54:34

标签: sql wpf

大家好我怎样才能更简单地编写下面的代码? ...在一个查询中有两个sql(OleDb)命令。

con.open();
cmd.CommandText = "DELETE * FROM Customers WHERE ID_C = 1";
cmd.ExecuteNonQuery();
cmd.Clone();
cmd.CommandText = "DELETE * FROM Books WHERE ID_B = 1";
cmd.ExecuteNonQuery();
cmd.Clone();
con.close();

1 个答案:

答案 0 :(得分:0)

如果您不需要受影响的行数(由ExecuteNonQuery返回),您只需将两个命令放在以“;”分隔的同一SQL中

con.open();
cmd.CommandText = "DELETE * FROM Customers WHERE ID_C = 1; DELETE * FROM Books WHERE ID_B = 1";
cmd.ExecuteNonQuery();
con.close();