/// <summary> /// 根据名称匹配分页查询 /// </summary> /// <param name="name"></param> /// <param name="page"></param> /// <param name="pageSize"></param> /// <returns></returns> public async Task <QueryResult <ClientSystemLoginEndpoint> > QueryByPage(string name, int page, int pageSize) { QueryResult <ClientSystemLoginEndpoint> result = new QueryResult <ClientSystemLoginEndpoint>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSystemToken(), 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 [ClientSystemLoginEndpoint] 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 [ClientSystemLoginEndpoint] WHERE [name] LIKE @name ORDER BY sequence OFFSET (@pagesize * (@currentpage - 1)) ROWS FETCH NEXT @pagesize ROWS ONLY;", StoreHelper.GetClientSystemLoginEndpointSelectFields(string.Empty)), Transaction = sqlTran }) { var 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); parameter = new SqlParameter("@name", SqlDbType.NVarChar, 500) { Value = $"{name.ToSqlLike()}%" }; command.Parameters.Add(parameter); await command.PrepareAsync(); SqlDataReader reader = null; await using (reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var endpoint = new ClientSystemLoginEndpoint(); StoreHelper.SetClientSystemLoginEndpointSelectFields(endpoint, reader, string.Empty); result.Results.Add(endpoint); } await reader.CloseAsync(); result.TotalCount = (int)command.Parameters["@count"].Value; result.CurrentPage = (int)command.Parameters["@currentpage"].Value; } } }); return(result); }
/// <summary> /// 根据名称查询 /// </summary> /// <param name="name"></param> /// <returns></returns> public async Task <ClientSystemLoginEndpoint> QueryByName(string name) { ClientSystemLoginEndpoint result = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSystemToken(), 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].[ClientSystemLoginEndpoint] WHERE name=@name;", StoreHelper.GetClientSystemLoginEndpointSelectFields(string.Empty)), Transaction = sqlTran }) { var parameter = new SqlParameter("@name", SqlDbType.NVarChar, 500) { Value = name }; command.Parameters.Add(parameter); await command.PrepareAsync(); SqlDataReader reader = null; await using (reader = await command.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { result = new ClientSystemLoginEndpoint(); StoreHelper.SetClientSystemLoginEndpointSelectFields(result, reader, string.Empty); } await reader.CloseAsync(); } } }); return(result); }