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 connection in c#

Database connection in c#

Scheduled Pinned Locked Moved C#
databasequestioncsharpsecurity
5 Posts 3 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.
  • N Offline
    N Offline
    nthomson
    wrote on last edited by
    #1

    I was wondering what is the best way to deal with database connections in c# windows application. Is it good practice or not to pass a connection string through the application and open a connection object when you need to query the database or is it better to keep a database connection open throughout the application. It is my understanding that it is better to open a connection when you need it. Is there any security risks in accessing a connection string through a static property in my application or is there a better way of accessing the connection string throughout the application? I would be interested to hear any-bodies suggestions. Thanks

    E 1 Reply Last reply
    0
    • N nthomson

      I was wondering what is the best way to deal with database connections in c# windows application. Is it good practice or not to pass a connection string through the application and open a connection object when you need to query the database or is it better to keep a database connection open throughout the application. It is my understanding that it is better to open a connection when you need it. Is there any security risks in accessing a connection string through a static property in my application or is there a better way of accessing the connection string throughout the application? I would be interested to hear any-bodies suggestions. Thanks

      E Offline
      E Offline
      Ed K
      wrote on last edited by
      #2

      We use a configuration file for the connection information. The strings are encrypted for security. if there is a change, we can adjust the connection strings without compiling and deploying. ed Regulation is the substitution of error for chance.

      N 1 Reply Last reply
      0
      • E Ed K

        We use a configuration file for the connection information. The strings are encrypted for security. if there is a change, we can adjust the connection strings without compiling and deploying. ed Regulation is the substitution of error for chance.

        N Offline
        N Offline
        nthomson
        wrote on last edited by
        #3

        The application uses the username and password that the user provides, not a default system account. Therefore it picks up the database security/permissions to tables/views etc. Any thoughts about securing the connection string in this sort system. Thanks

        R 1 Reply Last reply
        0
        • N nthomson

          The application uses the username and password that the user provides, not a default system account. Therefore it picks up the database security/permissions to tables/views etc. Any thoughts about securing the connection string in this sort system. Thanks

          R Offline
          R Offline
          Ray Cassick
          wrote on last edited by
          #4

          Build your connection string externally and use tokens for the username and password that can be replaced through code before they are used. I like to use [%USERNAME%] and [%PASSWORD%] as my tokens.


          Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.


          N 1 Reply Last reply
          0
          • R Ray Cassick

            Build your connection string externally and use tokens for the username and password that can be replaced through code before they are used. I like to use [%USERNAME%] and [%PASSWORD%] as my tokens.


            Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.


            N Offline
            N Offline
            nthomson
            wrote on last edited by
            #5

            At the moment I have a private member variable with I store the connection string in. I make this variable accessible through a public static property. What are the benefits of using the token methods? Is the static property a good or bad way to store the connection string. Getting back to the orignal post what do people think good practice is for handling database connections?

            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