我写了以下方法
public async Task<DataTable> ExecuteProcedureToDataTableAsync(string spName, object parameters, int? commandTimeout = null, bool userPrefix = false)
{
using (var connection = new SqlConnection(_ConnectionString))
{
string spNameWithPrefix = GetSpNameWithPrefix(spName, userPrefix);
var dt = new DataTable();
_Logger.Debug($"Executing Query: [{spNameWithPrefix}], with params:[{parameters.ToJsonString()}]");
dt.Load(await connection.ExecuteReaderAsync(spNameWithPrefix, parameters, commandTimeout: commandTimeout, commandType: CommandType.StoredProcedure));
_Logger.Debug($"Completed Query To DataTable: [{spNameWithPrefix}], result columnCount:[{dt.Columns.Count}], result row count:[{dt.Rows.Count}]");
return dt;
}
}
并像这样调用它:
using (var results = await ExecuteProcedureToDataTableAsync(StoredProcedureFullName, StoredProcedureParams, Timeout, userPrefix: false))
{
ExportReport(requestModel, results);
}
我得到一个例外:
ExecuteExer不支持MultiExec
是否不支持将spr与executeReader配合使用?