- Programmiersprache von VSC zu C# geändert - Framework von .NET Framework zu .NET geändert
68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using System.Data;
|
|
using Microsoft.Data.SqlClient;
|
|
using DigitalData.Modules.Logging;
|
|
|
|
namespace DigitalData.Modules.Database;
|
|
|
|
public class MSSQLServer
|
|
{
|
|
private readonly LogConfig _logConfig;
|
|
private readonly string _connectionString;
|
|
|
|
public MSSQLServer(LogConfig logConfig, string connectionString)
|
|
{
|
|
_logConfig = logConfig;
|
|
_connectionString = connectionString;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|