26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
72 lines
2.1 KiB
C#
Executable File
72 lines
2.1 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Data.SqlClient;
|
|
using DDTek.Oracle;
|
|
using NLog;
|
|
|
|
namespace DataModel.Db
|
|
{
|
|
public class DbManager
|
|
{
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
private static readonly Dictionary<string, DbConnectionString> connectionStrings = new Dictionary<string, DbConnectionString>();
|
|
|
|
|
|
public static void Initialize(List<DbConnectionString> dbConnectionStrings)
|
|
{
|
|
foreach (DbConnectionString dbConnectionString in dbConnectionStrings)
|
|
{
|
|
connectionStrings.Add(dbConnectionString.Name, dbConnectionString);
|
|
}
|
|
}
|
|
|
|
public static DbConnection GetConnection(string name)
|
|
{
|
|
if (!connectionStrings.TryGetValue(name, out var connectionString))
|
|
{
|
|
throw new Exception($"DB Connection definition for '{name}' not found.");
|
|
}
|
|
|
|
DbConnection connection = connectionString.GetConnection();
|
|
|
|
try
|
|
{
|
|
connection.Open();
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
string errorMessage = $"Failed to open DB connection to '{name}': {error.Message}.";
|
|
logger.Error(errorMessage);
|
|
throw new Exception(errorMessage, error);
|
|
}
|
|
|
|
return connection;
|
|
}
|
|
}
|
|
|
|
public enum DbTypes { SqlServer, Oracle }
|
|
|
|
public class DbConnectionString
|
|
{
|
|
public string Environment { get; set; }
|
|
public DbTypes DbType { get; set; }
|
|
public string Name { get; set; }
|
|
public string ConnectionString { get; set; }
|
|
|
|
public DbConnection GetConnection()
|
|
{
|
|
switch (DbType)
|
|
{
|
|
case DbTypes.Oracle:
|
|
return new OracleConnection(ConnectionString);
|
|
case DbTypes.SqlServer:
|
|
return new SqlConnection(ConnectionString);
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|