If you are truly not open to change this code will be another option and is not what I would ever use but wrote it for a Microsoft article to what not to do.
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace SqlServerLibrary.Classes
{
public sealed class SqlServerConnections
{
private static readonly Lazy<SqlServerConnections>
Lazy = new(() => new SqlServerConnections());
public static SqlServerConnections Instance => Lazy.Value;
private readonly Hashtable _connections = new ();
/// <summary>
/// Normally comes from appsetting.json or appConfig
/// </summary>
public string ConnectionString
=> "Data Source=.\\SQLEXPRESS;Initial Catalog=NorthWind2020;Integrated Security=True";
/// <summary>
/// Reset an existing connection string
/// </summary>
/// <param name="connectionString">An existing connection string</param>
public void Reset(string connectionString)
{
SqlConnection connection = null;
try
{
connection = (SqlConnection)_connections[connectionString];
connection.Dispose();
connection = null;
}
catch (Exception)
{
// ignored
}
}
/// <summary>
/// Used to reset all known connections to null
/// </summary>
public void ResetAll()
{
foreach (var cn in _connections)
{
SqlConnection connection = null;
try
{
connection = (SqlConnection)cn;
connection.Dispose();
connection = null;
}
catch (Exception)
{
// ignored
}
}
}
/// <summary>
/// Returns an open connection for connection string
/// </summary>
/// <param name="connectionString">Valid connection string</param>
/// <returns>Connection</returns>
public SqlConnection Connection(string connectionString)
{
SqlConnection connection = null;
var bNeedAdd = false;
try
{
connection = (SqlConnection)_connections[connectionString];
}
catch (Exception)
{
// ignored
}
if (connection == null)
{
bNeedAdd = true;
}
if (connection == null || connection.State == ConnectionState.Broken || connection.State == ConnectionState.Closed)
{
try
{
connection?.Dispose();
connection = null;
}
catch (Exception)
{
// ignored
}
connection = new SqlConnection();
}
if (connection.State == ConnectionState.Closed)
{
connection.ConnectionString = connectionString;
connection.Open();
}
if (bNeedAdd)
{
_connections.Add(connectionString, connection);
}
return connection;
}
}
}
Usage in backend
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using static SqlServerLibrary.Classes.SqlServerConnections;
namespace SqlServerLibrary.Classes
{
public class DataOperations
{
public static (Exception exception, List<string> nameList) ReadCategoryNames()
{
List<string> names = new ();
using var cn = Instance.Connection(Instance.ConnectionString);
const string selectStatement = "SELECT CategoryName FROM dbo.Categories";
using var cmd = new SqlCommand() { Connection = cn, CommandText = selectStatement };
try
{
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
names.Add(reader.GetString(0));
}
return (null, names);
}
catch (Exception e)
{
return (e, null);
}
}
}
}
Calling the above
public Demo()
{
var (exception, nameList) = DataOperations.ReadCategoryNames();
if (exception is null)
{
// use list
}
else
{
// report issue
// exception.Message
}
}