开发者

Executing multiple DbCommands in an open connection with Enterprise Library

开发者 https://www.devze.com 2022-12-23 21:37 出处:网络
How can you execute multiple DbCommands with one connection? Example: var db = DatabaseFactory.CreateDatabase开发者_开发技巧();

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


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 overload ExecuteNonQuery(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?

0

精彩评论

暂无评论...
验证码 换一张
取 消