public async Task <QueryResult <ScheduleActionGroup> > QueryByPage(string name, int page, int pageSize) { QueryResult <ScheduleActionGroup> result = new QueryResult <ScheduleActionGroup>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSchedule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, CommandText = string.Format(@"SET @currentpage = @page; SELECT @count = COUNT(*) FROM [dbo].[ScheduleActionGroup] WHERE name like @name; IF @pagesize * @page >= @count BEGIN SET @currentpage = @count / @pagesize; IF @count % @pagesize <> 0 BEGIN SET @currentpage = @currentpage + 1; END; IF @currentpage = 0 SET @currentpage = 1; END; ELSE IF @page < 1 BEGIN SET @currentpage = 1; END; SELECT {0} FROM [dbo].[ScheduleActionGroup] WHERE name like @name ORDER BY sequence OFFSET (@pagesize * (@currentpage - 1)) ROWS FETCH NEXT @pagesize ROWS ONLY;", StoreHelper.GetScheduleActionGroupSelectFields(string.Empty)), Transaction = sqlTran }) { var parameter = new SqlParameter("@name", SqlDbType.VarChar, 200) { Value = string.Format("{0}%", name.ToSqlLike()) }; command.Parameters.Add(parameter); parameter = new SqlParameter("@page", SqlDbType.Int) { Value = page }; command.Parameters.Add(parameter); parameter = new SqlParameter("@pagesize", SqlDbType.Int) { Value = pageSize }; command.Parameters.Add(parameter); parameter = new SqlParameter("@count", SqlDbType.Int) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); parameter = new SqlParameter("@currentpage", SqlDbType.Int) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); await command.PrepareAsync(); SqlDataReader reader = null; await using (reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var scheduleActionGroup = new ScheduleActionGroup(); StoreHelper.SetScheduleActionGroupSelectFields(scheduleActionGroup, reader, string.Empty); result.Results.Add(scheduleActionGroup); } await reader.CloseAsync(); result.TotalCount = (int)command.Parameters["@count"].Value; result.CurrentPage = (int)command.Parameters["@currentpage"].Value; } } }); return(result); }
public async Task <ScheduleActionGroup> QueryByName(string name) { ScheduleActionGroup result = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSchedule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, CommandText = string.Format(@"select {0} from [dbo].[ScheduleActionGroup] where [name] = @name", StoreHelper.GetScheduleActionGroupSelectFields(string.Empty)), Transaction = sqlTran }) { var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150) { Value = name }; command.Parameters.Add(parameter); await command.PrepareAsync(); SqlDataReader reader = null; await using (reader = await command.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { result = new ScheduleActionGroup(); StoreHelper.SetScheduleActionGroupSelectFields(result, reader, string.Empty); } await reader.CloseAsync(); } } }); return(result); }