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. base class connection string

base class connection string

Scheduled Pinned Locked Moved C#
database
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.
  • S Offline
    S Offline
    Stryder_1
    wrote on last edited by
    #1

    Hi, I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this. With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application. One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated. Thanks for any suggestions.

    P N 2 Replies Last reply
    0
    • S Stryder_1

      Hi, I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this. With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application. One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated. Thanks for any suggestions.

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

      That's done in the Data Access Layer. Are you saying you do it in your business objects?

      S 1 Reply Last reply
      0
      • S Stryder_1

        Hi, I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this. With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application. One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated. Thanks for any suggestions.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        class base
        {
        string m_connString;
        public string ConnString
        {
        get{ return m_ConnString;}
        private set{ m_ConnString = value;
        }

        public Base()
        {
        ConnString = default_value
        }

        pubic Base(string connString)
        {
        ConnString = connString;
        }
        }

        class Derived : Base
        {
        pubic Derived(string connString) : base(connString)
        {

        }
        

        }


        I know the language. I've read a book. - _Madmatt

        1 Reply Last reply
        0
        • P PIEBALDconsult

          That's done in the Data Access Layer. Are you saying you do it in your business objects?

          S Offline
          S Offline
          Stryder_1
          wrote on last edited by
          #4

          Nope, this is in a base class for the Data Access Layer code objects. Sorry if my post was not clear.

          P 1 Reply Last reply
          0
          • S Stryder_1

            Nope, this is in a base class for the Data Access Layer code objects. Sorry if my post was not clear.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Good. In my data access base classes[^] I hold a System.Data.IDbCommand, if I want the connection string I use this.cmd.Connection.ConnectionString; there's no need to store it a second time. I don't keep instantiating and throwing away connection and command instances -- that's wasteful (not that I've benchmarked it). But now I don't know what you mean by "each object can access the database independently". What objects? I have one data access class that is basically a wrapper around one Connection/Command (with specialized versions for the various databases I use). I can instantiate one for each database and I can clone it when I need an additional connection to a particular database.

            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