title: use one database connection in all classes
body: <p>I'm new to WPF (and application development in general), and I want to connect to my database and use that connection to query it in all my Model classes. I've written the connection string in the mainwindow code behind </p>
  
  <pre><code>public partial class MainWindow : Window
  {
      DBConnection db = new DBConnection();
  
      public MainWindow()
      {
          InitializeComponent();
          Connect();
      }
    ...etc
  </code></pre>
  
  <p>And the DBConnection class looks like this</p>
  
  <pre><code> class DBConnection
  {
      private OracleConnection connection;
      private OracleCommand command;
      private bool autoDisconnect;
  
  
      public void Connect(bool autoDisconnect = true)
      {
  
          this.autoDisconnect = autoDisconnect;
          try
          {
              if (connection == null)
              {
                  connection = new OracleConnection("CONNECTION DETAILS");
              }
              if (connection.State != ConnectionState.Open)
              {
                  connection.Open();
              }
          }
          catch (OracleException ex)
          { }
      }
  ..... etc
  </code></pre>
  
  <p>but I want to create the connection and the <code>db</code> object somewhere so that in all my models can just call the classes of <code>DBConnection</code> without having to create a new object every time</p>
  
  <p>for example, here's a model</p>
  
  <pre><code>public class Authentication
  {
      string username;
      string password;
  
      public static bool Authenticate(string UserName, string Password)
      {
          db.SetSQL("SQL STATEMENT HERE");
          db.ExecuteTransactions();
      }
  ...etc
  </code></pre>
  
  <p>where <code>SetSQL</code> and <code>ExecuteTransactions</code> are methods of the DBConnection Class</p>
  
  <p>So where and how can I (simply) connect to the database so it'll be constant throughout the whole application? Its just one window with varying UserControls so I thought it'd be ok to connect in the mainwindow code behind</p>