调整 Dapper.NET 中的 CommandTimeout?
我正在尝试通过 Dapper 通过存储过程运行 SQL 备份(我的应用程序的其余部分使用 Dapper,所以我更愿意让这部分也通过它运行)。它工作得很好,直到 CommandTimeout 启动。
using (var c = SqlConnection(connstring))
{
c.Open();
var p = new DynamicParameters();
// fill out p
c.Execute("xp_backup_database", p, commandType: CommandType.StoredProcedure);
}
我知道的唯一 CommandTimeout 设置是在 SqlCommand 中。有没有办法通过 Dapper 进行设置?
I'm trying to run SQL backups through a stored procedure through Dapper (the rest of my app uses Dapper so I'd prefer to keep this portion running through it as well). It works just fine until the CommandTimeout kicks in.
using (var c = SqlConnection(connstring))
{
c.Open();
var p = new DynamicParameters();
// fill out p
c.Execute("xp_backup_database", p, commandType: CommandType.StoredProcedure);
}
The only CommandTimeout setting I know of is in SqlCommand. Is there a way to set this via Dapper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,执行函数有多个版本。其中一个(或多个)包含 commandTimeout 参数:
取自 SqlMapper .cs
Yes, there are multiple versions of the Execute function. One (or more) of them contains the commandTimeout parameters:
Taken from SqlMapper.cs
原始问题的示例,添加了已接受的答案,以防有人需要。 (超时设置为60秒):
Example from original question with accepted answer added, in case anyone wants it. (Timeout is set to 60 seconds):
无需为所有查询/数据库调用设置命令超时。您可以像下面这样进行全局设置。
您可以在应用程序加载时或在数据库类构造函数中初始化此静态属性。
这有助于消除重复,并且如果您决定稍后更改它,您可以在一个地方更改一次。
There is no need to set command timeout for all queries/Db Calls. You can set it globally like below.
You can initialize this static property on the application load or in the database class constructor.
This helps in removing duplication, and in case you decide to change it later, you change it once in one place.
我能够使用 connection.Query 解决我的问题
直接设置超时时间
I was able to solve my problem using connection.Query
setting the timeout directly