diff --git a/src/Commons/Data/DataAccess.cs b/src/Commons/Data/DataAccess.cs
index 3bad94b1c..c43de7ba7 100644
--- a/src/Commons/Data/DataAccess.cs
+++ b/src/Commons/Data/DataAccess.cs
@@ -89,10 +89,10 @@ public DataTable GetDataTable(string sql)
///
/// Returns a DataTable object populated by a .
///
- public DataTable GetDataTable(DataAccessCommand cmd)
+ public DataTable GetDataTable(DataAccessCommand command)
{
var dataTable = new DataTable();
- ExecuteDataCommand(cmd, dataAdapter => dataAdapter.Fill(dataTable));
+ ExecuteDataCommand(command, dataAdapter => dataAdapter.Fill(dataTable));
return dataTable;
}
@@ -107,18 +107,18 @@ public DataSet GetDataSet(string sql)
///
/// Returns a DataSet object populated by a .
///
- public DataSet GetDataSet(DataAccessCommand cmd)
+ public DataSet GetDataSet(DataAccessCommand command)
{
var dataSet = new DataSet();
- ExecuteDataCommand(cmd, dataAdapter => dataAdapter.Fill(dataSet));
+ ExecuteDataCommand(command, dataAdapter => dataAdapter.Fill(dataSet));
return dataSet;
}
- private void ExecuteDataCommand(DataAccessCommand cmd, Action fillAction)
+ private void ExecuteDataCommand(DataAccessCommand command, Action fillAction)
{
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = CreateConnection();
using (dbCommand.Connection)
@@ -127,16 +127,12 @@ private void ExecuteDataCommand(DataAccessCommand cmd, Action fil
dataAdapter!.SelectCommand = dbCommand;
fillAction(dataAdapter);
- foreach (var parameter in cmd.Parameters)
- {
- if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- parameter.Value = dbCommand.Parameters[parameter.Name].Value;
- }
+ SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
}
@@ -184,28 +180,24 @@ public DataTable GetDataTable(ref DbConnection sqlConn, string sql)
/// ExecuteScalar command and returns the first column of the first row in the result set returned by the query.
/// All other columns and rows are ignored.
///
- public object? GetResult(DataAccessCommand cmd)
+ public object? GetResult(DataAccessCommand command)
{
object? scalarResult;
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = CreateConnection();
using (dbCommand.Connection)
{
scalarResult = dbCommand.ExecuteScalar();
- foreach (var param in cmd.Parameters)
- {
- if (param.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- param.Value = dbCommand.Parameters[param.Name].Value;
- }
+ SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
return scalarResult;
@@ -215,18 +207,18 @@ public DataTable GetDataTable(ref DbConnection sqlConn, string sql)
/// ExecuteScalar command and returns the first column of the first row in the result set returned by the query.
/// All other columns and rows are ignored.
///
- /// Command
+ /// Command
/// Open Connection
/// Transactions with Connection
/// Returns a DataTable object populated by a .
/// This method uses a by ref.
///
- public object? GetResult(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTransaction trans)
+ public object? GetResult(DataAccessCommand command, ref DbConnection sqlConn, ref DbTransaction trans)
{
object? scalarResult;
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = sqlConn;
dbCommand.Transaction = trans;
@@ -237,7 +229,7 @@ public DataTable GetDataTable(ref DbConnection sqlConn, string sql)
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
return scalarResult;
@@ -246,28 +238,24 @@ public DataTable GetDataTable(ref DbConnection sqlConn, string sql)
///
/// ExecuteNonQuery command in the database and return the number of affected records.
///
- public int SetCommand(DataAccessCommand cmd)
+ public int SetCommand(DataAccessCommand command)
{
int rowsAffected = 0;
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = CreateConnection();
using (dbCommand.Connection)
{
rowsAffected += dbCommand.ExecuteNonQuery();
- foreach (var parameter in cmd.Parameters)
- {
- if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- parameter.Value = dbCommand.Parameters[parameter.Name].Value;
- }
+ SetOutputParameters(command,dbCommand);
}
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
return rowsAffected;
@@ -278,7 +266,7 @@ public int SetCommand(DataAccessCommand cmd)
/// Author: Lucio Pelinson 14-04-2012
public int SetCommand(IEnumerable commands)
{
- int numberOfRowsAffected = 0;
+ var numberOfRowsAffected = 0;
DataAccessCommand? currentCommand = null;
var connection = CreateConnection();
@@ -324,28 +312,29 @@ public int SetCommand(string sql)
/// Author: Lucio Pelinson 14-04-2012
public int SetCommand(IEnumerable sqlList)
{
- var commandList = from string sql in sqlList select new DataAccessCommand(sql);
+ var commandList = sqlList.Select(sql => new DataAccessCommand(sql));
- int numberOfRowsAffected = SetCommand(commandList);
+ var numberOfRowsAffected = SetCommand(commandList);
+
return numberOfRowsAffected;
}
///
/// Execute the command in the database and return the number of affected records.
///
- /// Command
+ /// Command
/// Open Connection
/// Transactions with Connection
///
/// Returns a DataTable object populated by a .
/// This method uses a and a by ref.
///
- public int SetCommand(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTransaction trans)
+ public int SetCommand(DataAccessCommand command, ref DbConnection sqlConn, ref DbTransaction trans)
{
int numberOfRowsAffected = 0;
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = sqlConn;
dbCommand.Transaction = trans;
using (dbCommand.Connection)
@@ -355,7 +344,7 @@ public int SetCommand(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTra
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
return numberOfRowsAffected;
@@ -378,17 +367,17 @@ public int SetCommand(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTra
/// Retrieves the first record of the sql statement in a Hashtable object.
/// [key(database field), value(value stored in database)]
///
- /// Command
+ /// Command
///
/// Return a Hashtable Object.
/// If no record is found it returns null.
///
- public Hashtable? GetHashtable(DataAccessCommand cmd)
+ public Hashtable? GetHashtable(DataAccessCommand command)
{
Hashtable? retCollection = null;
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = CreateConnection();
using (dbCommand.Connection)
@@ -398,30 +387,23 @@ public int SetCommand(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTra
while (dr.Read())
{
retCollection = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
- int nQtd = 0;
-
- while (nQtd < dr.FieldCount)
+ for (var nQtd = 0; nQtd < dr.FieldCount; nQtd++)
{
string fieldName = dr.GetName(nQtd);
if (retCollection.ContainsKey(fieldName))
throw new DataAccessException($"[{fieldName}] field duplicated in get procedure");
retCollection.Add(fieldName, dr.GetValue(nQtd));
- nQtd += 1;
}
}
}
- foreach (var parameter in cmd.Parameters)
- {
- if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- parameter.Value = dbCommand.Parameters[parameter.Name].Value;
- }
+ SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
return retCollection;
@@ -431,17 +413,17 @@ public int SetCommand(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTra
/// Retrieves the records of the sql statement in a Dictionary object.
/// [key(database field), value(value stored in database)]
///
- /// Command
+ /// Command
///
/// Return a Dictionary Object.
/// If no record is found it returns null.
///
- public Dictionary? GetDictionary(DataAccessCommand cmd)
+ public Dictionary? GetDictionary(DataAccessCommand command)
{
Dictionary? retCollection = null;
try
{
- using var dbCommand = CreateDbCommand(cmd);
+ using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = CreateConnection();
using (dbCommand.Connection)
@@ -462,32 +444,17 @@ public int SetCommand(DataAccessCommand cmd, ref DbConnection sqlConn, ref DbTra
}
}
- foreach (var parameter in cmd.Parameters)
- {
- if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- parameter.Value = dbCommand.Parameters[parameter.Name].Value;
- }
+ SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
{
- throw GetDataAccessException(ex, cmd);
+ throw GetDataAccessException(ex, command);
}
return retCollection;
}
- ///
- /// Check if table exists in the database
- ///
- public bool TableExists(string tableName)
- {
- var ret = GetResult(GetTableExistsCommand(tableName));
- var result = ret as int? == 1;
-
- return result;
- }
-
/// Verify the database connection
/// True if the connection is successful.
/// Author: Lucio Pelinson 28-04-2014
@@ -568,25 +535,52 @@ public bool ExecuteBatch(string script)
return true;
}
-
- private static DataAccessCommand GetTableExistsCommand(string table)
+
+ public List> GetDictionaryList(DataAccessCommand command)
{
- const string sql = "SELECT COUNT(1) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @Table";
- var command = new DataAccessCommand
+ var dictionaryList = new List>();
+
+ try
{
- Sql = sql,
- Parameters =
+ using var dbCommand = CreateDbCommand(command);
+ dbCommand.Connection = CreateConnection();
+ using (dbCommand.Connection)
{
- new DataAccessParameter
+ using (var dataReader = dbCommand.ExecuteReader())
{
- Name = "@Table",
- Value = table
+ List columnNames = [];
+ for (var i = 0; i < dataReader.FieldCount; i++)
+ {
+ columnNames.Add(dataReader.GetName(i));
+ }
+
+ while ( dataReader.Read())
+ {
+ var dictionary = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
+ foreach (var columnName in columnNames)
+ {
+ var ordinal = dataReader.GetOrdinal(columnName);
+ var value = dataReader.IsDBNull(ordinal)
+ ? null
+ : dataReader.GetValue(ordinal);
+ dictionary[columnName] = value;
+ }
+
+ dictionaryList.Add(dictionary);
+ }
}
+
+ SetOutputParameters(command, dbCommand);
}
- };
+ }
+ catch (Exception ex)
+ {
+ throw GetDataAccessException(ex, command);
+ }
- return command;
+ return dictionaryList;
}
+
private static Exception GetDataAccessException(Exception ex, DataAccessCommand? cmd)
{
return GetDataAccessException(ex, cmd?.Sql ?? string.Empty, cmd?.Parameters);
@@ -622,14 +616,17 @@ private static Exception GetDataAccessException(
private DbCommand CreateDbCommand(DataAccessCommand command)
{
var dbCommand = _dbProviderFactory.CreateCommand();
+
if (dbCommand == null)
throw new ArgumentException(nameof(dbCommand));
if (string.IsNullOrEmpty(command.Sql))
throw new DataAccessException("Sql Command cannot be null or empty.");
+
dbCommand.CommandType = command.Type;
dbCommand.CommandText = command.Sql;
dbCommand.CommandTimeout = TimeOut;
+
foreach (var parameter in command.Parameters)
{
var dbParameter = CreateDbParameter(parameter);
@@ -653,67 +650,13 @@ private DbParameter CreateDbParameter(DataAccessParameter parameter)
return dbParameter;
}
-
- private static DataAccessCommand GetColumnExistsCommand(string tableName, string columnName)
- {
- var command = new DataAccessCommand
- {
- Sql =
- "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName"
- };
-
- command.AddParameter("@TableName", tableName, DbType.AnsiString);
- command.AddParameter("@ColumnName", columnName, DbType.AnsiString);
-
- return command;
- }
- public List> GetDictionaryList(DataAccessCommand cmd)
+ private static void SetOutputParameters(DataAccessCommand dataAccessCommand, DbCommand dbCommand)
{
- var dictionaryList = new List>();
-
- try
+ foreach (var parameter in dataAccessCommand.Parameters)
{
- using var dbCommand = CreateDbCommand(cmd);
- dbCommand.Connection = CreateConnection();
- using (dbCommand.Connection)
- {
- using (var dataReader = dbCommand.ExecuteReader())
- {
- List columnNames = [];
- for (var i = 0; i < dataReader.FieldCount; i++)
- {
- columnNames.Add(dataReader.GetName(i));
- }
-
- while ( dataReader.Read())
- {
- var dictionary = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
- foreach (var columnName in columnNames)
- {
- var ordinal = dataReader.GetOrdinal(columnName);
- var value = dataReader.IsDBNull(ordinal)
- ? null
- : dataReader.GetValue(ordinal);
- dictionary[columnName] = value;
- }
-
- dictionaryList.Add(dictionary);
- }
- }
-
- foreach (var param in cmd.Parameters.Where(param =>
- param.Direction is ParameterDirection.Output or ParameterDirection.InputOutput))
- {
- param.Value = dbCommand.Parameters[param.Name].Value;
- }
- }
- }
- catch (Exception ex)
- {
- throw GetDataAccessException(ex, cmd);
+ if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
+ parameter.Value = dbCommand.Parameters[parameter.Name].Value;
}
-
- return dictionaryList;
}
}
\ No newline at end of file
diff --git a/src/Commons/Data/DataAccessAsync.cs b/src/Commons/Data/DataAccessAsync.cs
index 652abd799..6051c7e9b 100644
--- a/src/Commons/Data/DataAccessAsync.cs
+++ b/src/Commons/Data/DataAccessAsync.cs
@@ -47,11 +47,7 @@ public async Task GetDataTableAsync(DataAccessCommand command, Cancel
var dataTable = new DataTable();
dataTable.Load(reader);
- foreach (var parameter in command.Parameters)
- {
- if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- parameter.Value = dbCommand.Parameters[parameter.Name].Value;
- }
+ SetOutputParameters(command, dbCommand);
return dataTable;
}
@@ -93,11 +89,7 @@ public async Task GetDataSetAsync(DataAccessCommand command, Cancellati
index++;
} while(!reader.IsClosed);
- foreach (var parameter in command.Parameters)
- {
- if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
- parameter.Value = dbCommand.Parameters[parameter.Name].Value;
- }
+ SetOutputParameters(command, dbCommand);
return dataSet;
}
@@ -116,54 +108,46 @@ public async Task GetDataSetAsync(DataAccessCommand command, Cancellati
}
///
- public async Task