public async Task <List <ListDictionary> > Command(string connString, string sqlQuery) { var insertDictionary = new List <ListDictionary>(); try { await using NpgsqlConnection conn = new NpgsqlConnection(connString); await conn.OpenAsync(); await using (var cmd = new NpgsqlCommand(sqlQuery, conn)) { cmd.Connection = conn; var lld = ParseSQL.parseInsertSQLReturnColumnsAndValues(sqlQuery); foreach (var listDictionary in lld) { ListDictionary localListDictionary = new ListDictionary(); foreach (DictionaryEntry entry in listDictionary) { var value = TemporalUtility.ConvertCurrentTimestampStringToDateTimeNowString(entry.Value.ToString()); cmd.Parameters.AddWithValue(value, entry.Key.ToString()); localListDictionary.Add(entry.Key.ToString(), value); } insertDictionary.Add(localListDictionary); } cmd.ExecuteNonQuery(); Console.WriteLine("Your insert statement was processed"); } } catch (Exception x) { Debug.WriteLine(x); } return(insertDictionary); }
public async Task <List <ListDictionary> > Command(string connString, string sqlQuery) { var updateDictionary = new List <ListDictionary>(); try { await using NpgsqlConnection conn = new NpgsqlConnection(connString); await conn.OpenAsync(); await using (var cmd = new NpgsqlCommand(sqlQuery, conn)) { cmd.Connection = conn; await using (var reader = await cmd.ExecuteReaderAsync()) { var colVal = ParseSQL.parseUpdateSQLReturnColumnsAndValues(sqlQuery); int i = 0; foreach (var key in colVal.Keys) { var ld = new ListDictionary(); var value = TemporalUtility.ConvertCurrentTimestampStringToDateTimeNowString(colVal[key].ToLower()); cmd.Parameters.Add(value, Util.GetTypeFromColumn(key)); ld.Add(key, value); updateDictionary.Add(ld); i++; } } } Console.WriteLine("The statement " + sqlQuery + " ran without throwing an error"); } catch (Exception ex) { ErrorMessages.MessageWhenUpdateValuesDoesNotHaveParentheses(ex); } return(updateDictionary); }