[c#] 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를 통해 이것을 설정하는 방법이 있습니까?



답변

예, 여러 버전의 실행 기능이 있습니다. 그중 하나 이상은 commandTimeout 매개 변수를 포함합니다.

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

SqlMapper.cs 에서 가져옴


답변

누군가가 원할 경우를 대비하여 수락 된 답변이 추가 된 원래 질문의 예. (타임 아웃은 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);
}


답변

모든 쿼리 / Db 호출에 대해 명령 시간 제한을 설정할 필요가 없습니다. 아래와 같이 전역 적으로 설정할 수 있습니다.

Dapper.SqlMapper.Settings.CommandTimeout = 0;

응용 프로그램로드 또는 데이터베이스 클래스 생성자에서이 정적 속성을 초기화 할 수 있습니다.

이렇게하면 중복을 제거하는 데 도움이되며 나중에 변경하려는 경우 한 번만 변경합니다.


답변

연결을 사용하여 문제를 해결할 수있었습니다.

int timeOutInSeconds = 60;
.
.
.
result = conn.Query<list>(stringQuery, new {parameters, ..}, null, true, timeOutInSeconds).ToList();


답변