Replaced LogConfig and direct connection string injection with IConfiguration. Connection string is now retrieved from configuration using the "Default" key. Removed LogConfig dependency and related code.
61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using System.Data;
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
namespace DigitalData.Modules.Database;
|
|
|
|
public class MSSQLServer(IConfiguration configuration)
|
|
{
|
|
private readonly string _connectionString = configuration.GetConnectionString("Default")
|
|
?? throw new InvalidOperationException("Connection string 'Default' not found.");
|
|
|
|
public static string DecryptConnectionString(string connectionString) => connectionString;
|
|
|
|
public SqlConnection GetConnection
|
|
{
|
|
get
|
|
{
|
|
var connection = new SqlConnection(_connectionString);
|
|
if (connection.State != ConnectionState.Open)
|
|
{
|
|
connection.Open();
|
|
}
|
|
|
|
return connection;
|
|
}
|
|
}
|
|
|
|
public DataTable GetDatatable(string sql)
|
|
{
|
|
using var connection = new SqlConnection(_connectionString);
|
|
using var command = new SqlCommand(sql, connection);
|
|
using var adapter = new SqlDataAdapter(command);
|
|
var table = new DataTable();
|
|
adapter.Fill(table);
|
|
return table;
|
|
}
|
|
|
|
public object? GetScalarValue(string sql)
|
|
{
|
|
using var connection = new SqlConnection(_connectionString);
|
|
using var command = new SqlCommand(sql, connection);
|
|
connection.Open();
|
|
return command.ExecuteScalar();
|
|
}
|
|
|
|
public bool ExecuteNonQuery(string sql)
|
|
{
|
|
using var connection = new SqlConnection(_connectionString);
|
|
using var command = new SqlCommand(sql, connection);
|
|
connection.Open();
|
|
return command.ExecuteNonQuery() > 0;
|
|
}
|
|
|
|
public bool ExecuteNonQuery(SqlCommand command)
|
|
{
|
|
using var connection = new SqlConnection(_connectionString);
|
|
command.Connection = connection;
|
|
connection.Open();
|
|
return command.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|