title: When to open/close connection to database?
body: <p>I am using PostgreSQL data server through VB.NET/ODBC on windows what seem's pretty reliable and highly usable solution for storing/manipulating data.<br>
  As a beginner here I have some doubts on when to open/close connection to database.  </p>
  
  <p>For example, I can open connection when program starts and keep it open during program execution what can be duration of few days, even months. In that case I transfer connection reference for use in all read/write functions in program.</p>
  
  <p>Or, in other case I can open/close connection for every function call or operation on database which not take a long time but brings many open/close situations.</p>
  
  <p>Now I would like to get recommendation from experienced users which way is better.  </p>
  
  <p>1) To keep opened connection all the time of program execution.<br>
  2) To connect/close every time when data is needed to read/save.</p>
  
  <p>Second but related question is when I opened connection to database like this:</p>
  
  <pre><code>    Dim conn As New OdbcConnection
      conn.ConnectionString = "Dsn=" + dbDsn + _
                              ";database=" + mydatabase + _
                              ";server=" + dbServer + _
                              ";port=" + dbPort + _
                              ";uid=" + dbUser + _
                              ";pwd=" + dbPass
      conn.Open()
      ... etc...
  </code></pre>
  
  <p>Is here a way to find later in program which exact database is opened using connection referenced by "conn"?</p>