public async Task <CommonMessageClientType> QueryByName(string name) { CommonMessageClientType type = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _commonQueueConnectionFactory.CreateReadForCommonQueue(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand commond = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, CommandText = string.Format(@"select {0},{1} from [CommonMessageClientType] as t join [CommonQueueProductEndpoint] as e on t.endpointid=e.id where t.[name]=@name", StoreHelper.GetCommonQueueConsumeEndpointSelectFields("t"), StoreHelper.GetCommonQueueProductEndpointSelectFields("e")) }) { var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150) { Value = name }; commond.Parameters.Add(parameter); await commond.PrepareAsync(); SqlDataReader reader = null; await using (reader = await commond.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { type = new CommonMessageClientType(); StoreHelper.SetCommonMessageClientTypeSelectFields(type, reader, "t"); type.Endpoint = new CommonQueueProductEndpoint(); StoreHelper.SetCommonQueueProductEndpointSelectFields(type.Endpoint, reader, "e"); } await reader.CloseAsync(); } } }); return(type); }
public async Task <QueryResult <CommonMessageClientType> > QueryByPage(string name, int page, int pageSize) { QueryResult <CommonMessageClientType> result = new QueryResult <CommonMessageClientType>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _commonQueueConnectionFactory.CreateReadForCommonQueue(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand commond = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, CommandText = string.Format(@" select @count= count(*) from [CommonMessageClientType] as t join [CommonQueueProductEndpoint] as e on t.endpointid=e.id where t.[name] like @name select {0},{1} from [CommonMessageClientType] as t join [CommonQueueProductEndpoint] as e on t.endpointid=e.id where t.[name] like @name order by t.[sequence] offset (@pagesize * (@page - 1)) rows fetch next @pagesize rows only;" , StoreHelper.GetCommonMessageClientTypeFields("t"), StoreHelper.GetCommonQueueProductEndpointSelectFields("e")) }) { var parameter = new SqlParameter("@page", SqlDbType.Int) { Value = page }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@pagesize", SqlDbType.Int) { Value = pageSize }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@name", SqlDbType.VarChar, 200) { Value = $"{name.ToSqlLike()}%" }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@count", SqlDbType.Int) { Direction = ParameterDirection.Output }; commond.Parameters.Add(parameter); await commond.PrepareAsync(); SqlDataReader reader = null; await using (reader = await commond.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var type = new CommonMessageClientType(); StoreHelper.SetCommonMessageClientTypeSelectFields(type, reader, "t"); type.Endpoint = new CommonQueueProductEndpoint(); StoreHelper.SetCommonQueueProductEndpointSelectFields(type.Endpoint, reader, "e"); result.Results.Add(type); } await reader.CloseAsync(); result.TotalCount = (int)commond.Parameters["@count"].Value; result.CurrentPage = page; } } }); return(result); }