在与 Enterprise Library 的开放连接中执行多个 DbCommand

发布于 2024-08-25 18:37:05 字数 484 浏览 3 评论 0原文

如何通过一个连接执行多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2024-09-01 18:37:05

每次调用 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 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文