using System; using System.Data; using System.Data.SqlClient; namespace DASFactoryDb { public partial class DbWrapper { /// /// For now DASFactory is always local db, but this lets us change it in the future /// public static bool _usingCentralizedDB = false; /// /// for now since DASFactor is always local db, this is true, but this lets us change it in the future /// public static bool _usingNTLMAuthentication = true; /// /// used to passively indicate connection status, does not perform any connection checking /// public static bool Connected { get => false; set {; } } /// /// returns a DbCommand interface to the DASFactory db /// /// internal static IDbCommand GetDASFactoryCommand() { var cmd = new SqlCommand(); cmd.Connection = new SqlConnection(Connection.GetLocalDASFactoryConnectionString()); cmd.Connection.Open(); return cmd; } //public static IDbCommand GetDASFactoryCommand() //{ // return GetDASFactoryCommand // (new SqlCommand(), new SqlConnection(Connection.GetLocalDASFactoryConnectionString())); //} //public static IDbCommand GetDASFactoryCommand(IDbCommand dbCmd, IDbConnection dbConnection) //{ // dbCmd.Connection = dbConnection; // dbCmd.Connection.Open(); // return dbCmd; //} public static DbWrapper Connection { get { lock (dbLock) { if (null == _dbOperations) { _dbOperations = new DbWrapper(); } } return _dbOperations; } } private string _localDASFactoryConnection = null; public void ResetLocalDASFactoryConnection() { _localDASFactoryConnection = null; _usingNTLMAuthentication = true; } public string GetLocalDASFactoryConnectionString() { lock (dbLock) { if (null != _localDASFactoryConnection) return _localDASFactoryConnection; if (string.IsNullOrWhiteSpace(Server)) { throw new Exception("Empty Server"); } _localDASFactoryConnection = _usingNTLMAuthentication ? $"Server={Server};Database=DASFactory;Trusted_Connection=TRUE;" : $"Server={Server};Database=DASFactory;User Id={Username};Password={Password};"; } return _localDASFactoryConnection; } public string Server { get; set; } = null; public string DBName { get; set; } = null; public string Username { get; set; } = ""; public string Password { get; set; } private static DbWrapper _dbOperations = null; private static readonly object dbLock = new object(); protected DbWrapper() { } /// /// returns device id if in db /// /// /// public static int GetDeviceId(string serialNumber) { if (!Connected) { throw new Exception("Not connected"); } using (var cmd = GetDASFactoryCommand()) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_IDASCommunicationTableGetRecordId"; cmd.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.NVarChar, 50) { Value = serialNumber }); var reader = cmd.ExecuteReader(); if (reader.Read()) { var id = Convert.ToInt32(reader["RecordId"]); return id; } return -1; } finally { cmd.Connection.Dispose(); } } } internal static void ProcessReturn(SqlParameter errorNumber, SqlParameter errorMessage) { if (null != errorNumber) { if (0 != Convert.ToInt32(errorNumber.Value)) { throw new Exception($"{errorNumber.Value}{(string)errorMessage.Value}"); } } } } }