C# 在 Dapper.NET 中调整 CommandTimeout?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8794858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:28:32  来源:igfitidea点击:

Adjusting CommandTimeout in Dapper.NET?

c#.nettimeoutdapper

提问by sh-beta

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.

我正在尝试通过 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);
}

The only CommandTimeout setting I know of is in SqlCommand. Is there a way to set this via Dapper?

我所知道的唯一 CommandTimeout 设置是在 SqlCommand 中。有没有办法通过 Dapper 设置它?

采纳答案by jzacharuk

Yes, there are multiple versions of the Execute function. One (or more) of them contains the commandTimeout parameters:

是的,有多个版本的 Execute 函数。其中一个(或多个)包含 commandTimeout 参数:

public static int Execute(this IDbConnection cnn, string sql, 
                dynamic param = null, IDbTransaction transaction = null, 
                            int? commandTimeout = null, CommandType? commandType = null)

Taken from SqlMapper.cs

取自SqlMapper.cs

回答by Adrian Carr

Example from original question with accepted answer added, in case anyone wants it. (Timeout is set to 60 seconds):

来自原始问题的示例并添加了已接受的答案,以防万一有人想要。(超时设置为 60 秒):

using (var c = SqlConnection(connstring))
{
    c.Open();
    var p = new DynamicParameters();
    // fill out p

    c.Execute("xp_backup_database", p, commandTimeout: 60, 
                                       commandType: CommandType.StoredProcedure);
}