title: Object-Oriented Programming and databases
body: <p>I am learning about object-oriented programming. My application is based on two MySQL databases. So I get some records, insert some data to my databases very often. </p>
  
  <p>Is it correct to have a class called <code>MySQL</code>? This is a very simple class, there are only two methods - <code>Connect()</code> and <code>Disconnect()</code>. This class is shown below:</p>
  
  <pre><code>Imports MySql.Data.MySqlClient
  
  Public Class MySQL 
      Private csJablotron As String = My.Settings.csJablotron
      Private csBCS As String = My.Settings.csBCS
      Private _connection As MySqlConnection
  
      Public ReadOnly Property Connection() As MySqlConnection
          Get
              Return _connection 
          End Get
      End Property
  
  
      Public Sub Connect(shop As String)
  
          Select Case shop
              Case "jablotron"
                  _connection = New MySqlConnection(csJablotron)
              Case "bcs"
                  _connection = New MySqlConnection(csBCS)
              Case Else
                  MessageBox.Show("There is no shop with this name.")
                  Exit Sub
          End Select
  
          Try
              _connection .Open()
          Catch ex As MySqlException
              MessageBox.Show(ex.Message)
          Catch ex As Exception
              MessageBox.Show(ex.Message)
          End Try
      End Sub
  
  
      Public Sub Disconnect()
          Try
              _connection .Dispose()
          Catch ex As MySqlException
              MessageBox.Show(ex.Message)
          Catch ex As Exception
              MessageBox.Show(ex.Message)
          End Try
      End Sub
  End Class
  </code></pre>
  
  <p>And then in my main program when I need to use my database I just simply do that:</p>
  
  <pre><code>Try
      mySql.Connect("bcs")    'mySql is an object of MySQL class
      ... 
      'here I select some records and do something with them...
  Catch
      MessageBox.Show("Error")
  Finally
      mySql.Disconnect()
  End Try
  </code></pre>
  
  <p><strong>Is it correct in object-oriented programming?</strong> Or better is just always use a <code>using</code> statement when I need a database connection, and create a connection string there and don't even use this class? I know that this is a theoretical question but I am very curious what is better.</p>