在与 Enterprise Library 的开放连接中执行多个 DbCommand
如何通过一个连接执行多个 DbCommand?
示例:
var db = DatabaseFactory.CreateDatabase();
var dbCommand = db.GetSqlStringCommand(InsertCommandText);
...
db.ExecuteNonQuery(dbCommand);
现在,我希望能够执行多个 dbCommand。例如在伪代码中:
var db = DatabaseFactory.CreateDatabase();
var dbCommand1 = db.GetSqlStringCommand(InsertCommandText);
...
var dbCommand1 = db.GetSqlStringCommand(InsertCommandText);
...
Adding both commands to db
Executing them
How can you execute multiple DbCommands with one connection?
Example:
var db = DatabaseFactory.CreateDatabase();
var dbCommand = db.GetSqlStringCommand(InsertCommandText);
...
db.ExecuteNonQuery(dbCommand);
Now, I want to be able to Execute multiple dbCommands. For instance in pseudo kind of code:
var db = DatabaseFactory.CreateDatabase();
var dbCommand1 = db.GetSqlStringCommand(InsertCommandText);
...
var dbCommand1 = db.GetSqlStringCommand(InsertCommandText);
...
Adding both commands to db
Executing them
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用 ExecuteNonQuery(DbCommand) 都可能使用新连接。如果您想确保始终使用相同的连接,可以使用重载
ExecuteNonQuery(DbCommand, DbTransaction)
。如果您传入事务,企业库将使用与该事务关联的连接。当然,如果不需要,您可能不想使用事务。也许了解为什么您只想使用一个连接会有所帮助?
Each call to
ExecuteNonQuery(DbCommand)
will potentially use a new connection. If you wanted to ensure that you were always using the same connection you could use the overloadExecuteNonQuery(DbCommand, DbTransaction)
. If you pass in a transaction Enterprise Library will use the connection associated with the transaction. Of course, you may not want to use a transaction if it is not required.Maybe it would help to know why you want to only use one connection?