using System; using System.Diagnostics; using System.Data; using System.Data.OleDb; using JRCookbookBusiness; public class clsDatabaseLayerBase : IDisposable { private OleDbConnection m_dbcnctConnection; private bool disposedValue; //public static String DockerHostMachineIpAddress => Dns.GetHostAddresses(new Uri("http://host.docker.internal").Host)[0].ToString(); protected bool OpenDatabaseConnection() { try { if (m_dbcnctConnection != null) return true; m_dbcnctConnection = new OleDbConnection(); // m_dbcnctConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=OrganLit;server=REUSS\SQLEXPRESS;Connect Timeout=30" // m_dbcnctConnection.ConnectionString = "Persist Security Info=False;Integrated Security=False;User ID=OrganLitDBLogin;Password=OrganLitDBL0gin7;database=OrganLit;server=SIRJON\\REUSSSQL;Connect Timeout=30" //m_dbcnctConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=\"" + "C:\\ReussData\\lc50.mdb" + "\""; m_dbcnctConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ReussData\\lc50.mdb"; //m_dbcnctConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + "C:\\ReussData\\lc50.mdb"; //m_dbcnctConnection.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=C:/ReussData/lc50.mdb; Uid = Admin; Pwd =; "; //m_dbcnctConnection.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=C:/ReussData/lc50.mdb;"; // m_dbcnctConnection.ConnectionString = "Data Source=tcp:bu7zkus8nd.database.windows.net,1433;Initial Catalog=OrganLitDB;User Id=OrganLitDBUser@bu7zkus8nd;Password=ILamfi0gAdm;"; m_dbcnctConnection.Open(); return true; } catch (OleDbException e) { m_dbcnctConnection = null; Debug.Print("Error in OpenDatabaseConnection: " + e.Message); //return false; throw; } catch (Exception e) { m_dbcnctConnection = null; Debug.Print("Error in OpenDatabaseConnection: " + e.Message); //return false; throw; } } protected bool CloseDatabaseConnection() { try { if (m_dbcnctConnection != null) { m_dbcnctConnection.Close(); m_dbcnctConnection.Dispose(); m_dbcnctConnection = null; } return true; } catch (OleDbException e) { Debug.Print("Error in CloseDatabaseConnection: " + e.Message); return false; } catch (Exception e) { Debug.Print("Error in CloseDatabaseConnection: " + e.Message); return false; } } protected DataSet ExecuteDataSet(OleDbCommand objOleDbCommand) { try { if (OpenDatabaseConnection() == false) throw new ApplicationException("Database not opened"); objOleDbCommand.Connection = m_dbcnctConnection; OleDbDataAdapter ldbdaDataAdapter = new OleDbDataAdapter(); ldbdaDataAdapter.SelectCommand = objOleDbCommand; DataSet ldbdsDataSet = new DataSet(); ldbdaDataAdapter.Fill(ldbdsDataSet); return ldbdsDataSet; } catch (OleDbException e) { //Debug.Print("Error in ExecuteDataSet: Procedure=" + e.Procedure + Constants.CRLF + "Error=" + e.Message); Debug.Print("Error in ExecuteDataSet: Error=" + e.Message); //return null/* TODO Change to default(_) if this is not a reference type */; throw; } catch (Exception e) { Debug.Print("Error in ExecuteDataSet: " + e.Message); //return null/* TODO Change to default(_) if this is not a reference type */; throw; } //finally //{ // if (m_dbcnctConnection != null) // { // m_dbcnctConnection.Dispose(); // m_dbcnctConnection = null; // } //} } protected Int32 ExecuteNonQuery(OleDbCommand objOleDbCommand) { // Returns rows affected try { if (OpenDatabaseConnection() == false) throw new ApplicationException("Database not opened"); objOleDbCommand.Connection = m_dbcnctConnection; Int32 lintRowsAffected = objOleDbCommand.ExecuteNonQuery(); return lintRowsAffected; } catch (OleDbException e) { //Debug.Print("Error in ExecuteNonQuery: Procedure=" + e.Procedure + Constants.CRLF + "Error=" + e.Message); Debug.Print("Error in ExecuteNonQuery: Error=" + e.Message); //return 0; throw; } catch (Exception e) { Debug.Print("Error in ExecuteNonQuery: " + e.Message); //return 0; throw; } } protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects) if (m_dbcnctConnection != null) { m_dbcnctConnection.Dispose(); m_dbcnctConnection = null; } } // TODO: free unmanaged resources (unmanaged objects) and override finalizer // TODO: set large fields to null disposedValue = true; } } // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources // ~clsDatabaseLayerBase() // { // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method // Dispose(disposing: false); // } void IDisposable.Dispose() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); } }