Added sql server ping feature, check for online server (#51)

This commit is contained in:
Bobbie Hodgetts
2026-05-11 22:21:39 +01:00
committed by GitHub
parent db9a2b9ccc
commit 89ec6476fc
10 changed files with 188 additions and 195 deletions
+46 -60
View File
@@ -1,92 +1,78 @@
using System;
using FikaAmazonAPI.AmazonSpApiSDK.Models.Services;
using System;
using System.Configuration;
namespace bnhtrade.Core.Data.Database
{
/// this class needs a sort out. Ideally it shoud be called what it is, a connection string builder, and
/// it should expose a method to serve the connection string--rather than class inheritance and using a property setter
/// something to do once there aren't so many open git branches
///
public class Connection
{
//protected readonly string SqlConnectionString;
private Model.Credentials.bnhtradeDB _dbCredentials;
private string _server;
private string _user;
private string _userPassword;
private string _database = "e2A";
private bool _persistSecurityInfo = true;
private bool _multipleActiveResultSets = true;
private bool _encrypt = true;
private uint _connectRetryInterval;
private uint _connectRetryCount;
private uint _connectTimeout;
protected string SqlConnectionString
{
get { return _dbCredentials.ConnectionString; }
}
internal string SqlConnectionString { get; private set; }
public Connection()
/// <summary>
///
/// </summary>
/// <param name="connectRetryInterval">Retry interval in seconds, must be 5-60</param>
/// <param name="connectTimeout">Timeout length in seconds (0 is indefinitely)</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="Exception"></exception>
public Connection(uint connectRetryInterval = 10, uint connectRetryCount = 6, uint connectionTimeout = 60)
{
var config = new Config().GetConfiguration();
if (connectRetryInterval < 5 || connectRetryInterval > 60)
{
// these are limits set by the sql server
throw new ArgumentOutOfRangeException("ConnectRetryInterval must be from 5 to 60 seconds");
}
_connectRetryInterval = connectRetryInterval;
_connectRetryCount = connectRetryCount;
_connectTimeout = connectionTimeout;
// attempt to retrive credentials from app.local.config
// retrive credentials from app.local.config
var config = new Config().GetConfiguration();
try
{
string dataSource = config.AppSettings.Settings["DbDataSource"].Value;
string userId = config.AppSettings.Settings["DbUserId"].Value;
string pass = config.AppSettings.Settings["DbUserPassword"].Value;
_server = config.AppSettings.Settings["DbDataSource"].Value;
_user = config.AppSettings.Settings["DbUserId"].Value;
_userPassword = config.AppSettings.Settings["DbUserPassword"].Value;
// check
if (string.IsNullOrEmpty(dataSource))
if (string.IsNullOrEmpty(_server))
{
throw new ArgumentException("Could not retrive 'DbDataSource' from config file");
}
else if (string.IsNullOrEmpty(userId))
else if (string.IsNullOrEmpty(_user))
{
throw new ArgumentException("Could not retrive 'DbUserId' from config file");
}
else if (string.IsNullOrEmpty(pass))
else if (string.IsNullOrEmpty(_userPassword))
{
throw new ArgumentException("Could not retrive 'DbUserPassword' from config file");
}
var dbCredentials = new bnhtrade.Core.Model.Credentials.bnhtradeDB(dataSource, userId, pass);
this._dbCredentials = dbCredentials;
}
catch (Exception ex)
{
throw new Exception("Unable to retirve DB credentials: " + ex.Message);
}
}
private void ConnectionOld()
{
// attempt to retrive credentials from app.local.config
try
{
string dataSource = ConfigurationManager.AppSettings["DbDataSource"];
string userId = ConfigurationManager.AppSettings["DbUserId"];
string pass = ConfigurationManager.AppSettings["DbUserPassword"];
// check
if (string.IsNullOrEmpty(dataSource))
{
throw new ArgumentException("Could not retrive 'DbDataSource' from config file");
}
else if (string.IsNullOrEmpty(userId))
{
throw new ArgumentException("Could not retrive 'DbUserId' from config file");
}
else if (string.IsNullOrEmpty(pass))
{
throw new ArgumentException("Could not retrive 'DbUserPassword' from config file");
}
var dbCredentials = new bnhtrade.Core.Model.Credentials.bnhtradeDB(dataSource, userId, pass);
this._dbCredentials = dbCredentials;
}
catch(Exception ex)
{
throw new Exception("Unable to retirve DB credentials: " + ex.Message);
}
}
public Connection(Model.Credentials.bnhtradeDB dbCredentials)
{
// setup sql parameters
if (dbCredentials == null)
{
throw new Exception("DB credentials object is null");
}
this._dbCredentials = dbCredentials;
// build connection string
SqlConnectionString = "Server=" + _server + ";Database=" + _database + ";PersistSecurityInfo=" + _persistSecurityInfo.ToString()
+ ";User=" + _user + ";Password=" + _userPassword + ";MultipleActiveResultSets=" + _multipleActiveResultSets.ToString()
+ ";ConnectRetryInterval=" + _connectRetryInterval + ";ConnectRetryCount=" + _connectRetryCount + ";Timeout=" + _connectTimeout
+ ";Encrypt=" + _encrypt.ToString() +";";
}
}
}