Replaced DigitalData.Modules.Database with EnvelopeGenerator.ServiceHost.Jobs.Infrastructure in using statements and class namespaces. All database operations are now organized under the new namespace for better clarity and project structure.
61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using System.Data;
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
namespace EnvelopeGenerator.ServiceHost.Jobs.Infrastructure;
|
|
|
|
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;
|
|
}
|
|
}
|