protected async Task <List <T> > DeserializerAsync <T>(MySql.Data.MySqlClient.MySqlCommand command, CancellationToken canToken) where T : new() { using (var dataRead = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, canToken)) { var func = DeserializerManager.GetInstance().GetFuncForType <T>(dataRead); return(func(dataRead)); } }
public async Task <HealthCheckResult> Run() { HealthCheckStatus status; string error; try { bool hasResult = false; using (var conn = new MySql.Data.MySqlClient.MySqlConnection(_connectionString)) { using (var cmd = new MySql.Data.MySqlClient.MySqlCommand()) { cmd.Connection = conn; cmd.CommandText = "SELECT NOW();"; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandTimeout = _connectTimeout; await conn.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(); while (await reader.ReadAsync()) { hasResult |= true; } conn.Close(); } } status = hasResult ? HealthCheckStatus.Pass : HealthCheckStatus.Fail; error = null; } catch (Exception exception) { status = HealthCheckStatus.Fail; error = exception.Message; } return(new HealthCheckResult(nameof(MySqlConnectionHealthCheck), _description, error, status.ToString())); }
public static async Task <SqlDataReader> ExecuteReaderAsync(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return(rdr); } catch { conn.Close(); throw; } }
public async Task <EntityCollection <T> > GetRecords_Async <T>(Base.RequestFilters filters = null, Base.DataOrder Order = null, int PageSize = 0, int Page = 0) where T : class, IDataEntity { var currentType = typeof(T); var records = new EntityCollection <T>(); //first set the source where we get the data //if there is not any Table attribute we set the source as the type name DataInterface.Attribbutes.Table TableAttribute = (DataInterface.Attribbutes.Table)(from attr in currentType.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Table) select attr).FirstOrDefault(); using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Source)) { await connection.OpenAsync(); try { StringBuilder Query = new StringBuilder(); if (TableAttribute == null) { Query.Append($"Select * from {currentType.Name } where 1=1 "); } else { Query.Append($"Select * from {TableAttribute.Name} where 1=1 "); } MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand(); BuildWhereStaTement(filters, Query, com); if (Order != null) { Query.Append($" order by {Order.Field} {Order.Order}"); } if (Page != 0) { Query.Append($" OFFSET (({Page} - 1) * {PageSize }) ROWS FETCH NEXT {PageSize} ROWS ONLY; "); BuildCountPagingQuery(filters, PageSize, currentType, records, TableAttribute, connection); } com.Connection = connection; com.CommandText = Query.ToString(); var result = await com.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection); if (result.HasRows) { FillresultsNew(currentType, records, result); } result.Close(); } catch (Exception ex) { if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } throw; } } if (Page != 0) { records.Pager.CurrentPage = Page; } else { records.Pager.TotalRecords = records.Count; } records.Pager.PageRecords = records.Count; return(records); }