This module provides core database connection and user authentication functionality for the DataPRO system, abstracting both remote SQL Server connections and local SQL Server LocalDB instance management. It defines interfaces and implementations for establishing database connections (including dynamic LocalDB instance lifecycle management), authenticating users via password or pre-hashed credentials, and tracking active connections and logged-in users. The module serves as the foundational layer for all database interactions in the system, ensuring consistent connection handling, user session tracking, and error reporting.
-
bool UseNTLMAuthentication { get; set; }
Indicates whether to use Windows Authentication (NTLM) instead of SQL authentication. Defaults to true.
-
string DbUser { get; set; }
Username for SQL authentication. Defaults to "DataPROUser".
-
string DbUserPassword { get; set; }
Password for SQL authentication. Defaults to "DTSSealBeachHQ".
-
string InstanceName { get; set; }
Name of the LocalDB instance to use. Defaults to "DataPROInstance".
-
string DbServer { get; set; }
Server name for remote DB connections. Defaults to @"(localdb)\DataPROInstance".
-
string DbName { get; set; }
Name of the database to connect to. Defaults to "DataPRO".
-
bool UsingCentralizedDb { get; set; }
true if connecting to a centralized (remote) database; false for LocalDB.
-
string DbFolderPath { get; set; }
Path to directory containing .mdf and .ldf files for LocalDB attachment.
-
string AttachDbsBatPath { get; set; }
Full path to attach.bat script used to attach databases via sqlcmd.exe.
-
string ODBCToolsPath { get; set; }
Path to directory containing sqlcmd.exe.
-
string SqlDbPath { get; set; }
Path to directory containing SqlLocalDB.exe.
-
int ClientDbVersion { get; set; }
Database version expected by the client (code-defined). Must be initialized by clients.
-
int ConnectionDbVersion { get; set; }
Database version actually in use (e.g., from metadata). Must be initialized by clients.
-
string GetConnectionString()
Returns a connection string for SqlClient. Uses Windows Auth if UseNTLMAuthentication is true; otherwise uses SQL auth with DbUser/DbUserPassword. Caches result in internal _Connection field.
-
IConnectionDetails Clone()
Returns a deep copy of the current instance.
-
IConnectionDetails[] GetActiveConnections()
Returns array of all currently active IConnectionDetails instances.
-
ulong LoginUserHash(IConnectionDetails connection, string user, string hash, out IUserDbRecord userObject)
Authenticates user using a pre-hashed password (SHA256 base64-encoded). Returns ERROR_SUCCESS on success, ERROR_LOGINFAILED if hash mismatch, ERROR_UNKNOWN on exception.
-
ulong LoginUser(IConnectionDetails connection, string user, string password, out IUserDbRecord userObject)
Authenticates user using plaintext password. Computes SHA256 hash of "{password}_{user}", base64-encodes it, and compares to stored hash. Returns same error codes as LoginUserHash.
-
ulong ConnectToDb(IConnectionDetails details)
Establishes database connection. For LocalDB (UsingCentralizedDb == false), manages instance lifecycle: stops/deletes/creates/starts instance, then attaches databases. Returns ERROR_SUCCESS on success, ERROR_UNKNOWN on failure.
-
bool IsUserLoggedIn(IUserDbRecord user, IConnectionDetails connection)
Returns true if user is logged in to connection.
-
Tuple<IUserDbRecord, IConnectionDetails>[] GetLoggedInUsers()
Returns array of all logged-in user/connection pairs.
-
void ClearConnections()
Removes all logged-in users and all active connections.
-
ulong ConnectToDb(IConnectionDetails details)
Public entry point for IConnections.ConnectToDb. Wraps Connect() in logging and exception handling.
-
IConnectionDetails[] GetActiveConnections()
Returns snapshot of active connections.
-
ulong LoginUserHash(...) / ulong LoginUser(...)
Implement IConnections methods. LoginUser computes SHA256 hash of "{password}_{user}" (UTF-8 encoded) and compares to foundUser.Password.
-
bool IsUserLoggedIn(...)
Checks _loggedInUsers list for matching user/connection pair.
-
Tuple<IUserDbRecord, IConnectionDetails>[] GetLoggedInUsers()
Returns snapshot of _loggedInUsers.
-
void ClearConnections()
Clears _loggedInUsers and _connections lists.
-
private ulong Connect(IConnectionDetails details)
Core connection logic: routes to ConnectRemote or ConnectLocal, clones details, and adds to _connections on success.
-
private ulong ConnectLocal(IConnectionDetails details)
Manages LocalDB instance lifecycle:
- Stops existing instance (ignores "instance doesn’t exist" errors)
- Deletes existing instance (ignores "instance doesn’t exist" errors)
- Creates new instance
- Starts instance
- Attaches databases (
DataPRO and ISO)
Returns ERROR_SUCCESS or ERROR_UNKNOWN.
-
private void AttachDatabases(IConnectionDetails details, LogDelegate log)
Calls AttachDatabase for details.DbName and "ISO".
-
private static void AttachDatabase(...)
Executes attach.bat script with parameters: dbName, dbFileName, logFileName, fullSqlcmdPath. Throws Exception on non-empty result.
-
internal static ulong GetSqlCommand(IConnectionDetails con, out SqlCommand cmd, string commandText = "")
Helper to create/open SqlCommand using con.GetConnectionString(). Returns ERROR_SUCCESS or ERROR_UNKNOWN.