问题
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?
回答1:
Yes, there are multiple versions of the Execute function. One (or more) of them contains the commandTimeout parameters:
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
回答2:
Example from original question with accepted answer added, in case anyone wants it. (Timeout is set to 60 seconds):
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);
}
来源:https://stackoverflow.com/questions/8794858/adjusting-commandtimeout-in-dapper-net