/// <summary> /// OData function for clearing log records. /// </summary> /// <param name="dateTime">Stringed <see cref="DateTimeOffset"/> date. Delete all records older that date.</param> /// <returns>Success operation return <c>true</c>.</returns> private static bool ClearLogRecords(string dateTime) { DateTimeOffset date; if (!DateTimeOffset.TryParse(dateTime, out date)) { throw new ArgumentException("Invalid date format", nameof(dateTime)); } SQLDataService ds = (SQLDataService)DataServiceProvider.DataService; IDbConnection connection = ds.GetConnection(); connection.Open(); try { IDbCommand command = connection.CreateCommand(); string tableName = Information.GetClassStorageName(typeof(ApplicationLog)); string timestampColumnName = Information.GetPropertyStorageName( typeof(ApplicationLog), Information.ExtractPropertyName <ApplicationLog>(a => a.Timestamp)); command.CommandText = $"DELETE FROM {tableName} WHERE {timestampColumnName} <= @timestamp"; SqlParameter timestampParameter = new SqlParameter("@timestamp", SqlDbType.DateTimeOffset) { Value = date }; command.Parameters.Add(timestampParameter); command.ExecuteScalar(); return(true); } finally { // Close the connection if that's how we got it. connection.Close(); } }
protected void ExecuteSqlQueryButtom_OnClick(object sender, EventArgs e) { SQLDataService ds = (SQLDataService)DataServiceProvider.DataService; IDbConnection connection = ds.GetConnection(); try { connection.Open(); var command = connection.CreateCommand(); command.CommandText = "SELECT Город FROM Абитуриент GROUP BY Город ORDER BY COUNT(*) DESC"; var result = command.ExecuteScalar(); } catch (Exception ex) { } finally { connection.Close(); } }
protected void ExecuteSqlQueryButton_OnClick(object sender, EventArgs e) { SQLDataService ds = (SQLDataService)DataServiceProvider.DataService; IDbConnection connection = ds.GetConnection(); try { connection.Open(); var command = connection.CreateCommand(); command.CommandText = "select count(*) from Преподаватель"; var result = command.ExecuteScalar(); QueryResultLabel.Text = result.ToString(); } catch (Exception ex) { QueryResultLabel.Text = ex.ToString(); } finally { connection.Close(); } }