Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Database connections

Database connections

Scheduled Pinned Locked Moved C#
databasepostgresqlquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin Marvinski
    wrote on last edited by
    #1

    I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)

    P B P 3 Replies Last reply
    0
    • M Martin Marvinski

      I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      I think so; instantiate the connection once, open it and close it as needed, pass it around as needed.

      1 Reply Last reply
      0
      • M Martin Marvinski

        I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)

        B Offline
        B Offline
        beatles1692
        wrote on last edited by
        #3

        Hi Here's my suggestion Have a DataHelper(or DataManager) class that is responsible to do DB management and other stuffs (like opening connections) make it singleton. It can have a OpenConnection method that opens a new connection and returns it every time you want an open connection.For example using(SqlDBConnection cnn=DataManager.Instance.OpenConnection()) { //do something with connection. cnn.Close() }//Since SqlDBConnection is IDisposable it will dispose automatically Hamidian

        B 1 Reply Last reply
        0
        • B beatles1692

          Hi Here's my suggestion Have a DataHelper(or DataManager) class that is responsible to do DB management and other stuffs (like opening connections) make it singleton. It can have a OpenConnection method that opens a new connection and returns it every time you want an open connection.For example using(SqlDBConnection cnn=DataManager.Instance.OpenConnection()) { //do something with connection. cnn.Close() }//Since SqlDBConnection is IDisposable it will dispose automatically Hamidian

          B Offline
          B Offline
          beatles1692
          wrote on last edited by
          #4

          Oh I forgot that you were not using SQL Server.Then replace the SqlDbConnection with OleDbConnection. :)

          1 Reply Last reply
          0
          • M Martin Marvinski

            I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            It doesn't seem to bad to me. This is (in effect) the way that you would be working with the connections if you were working in a technology like ASP.NET. If the connection is pulled from the Connection Pool then you shouldn't have too many problems.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups