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. Global variable in C#

Global variable in C#

Scheduled Pinned Locked Moved C#
csharpdatabasehelp
6 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.
  • A Offline
    A Offline
    aahamdan
    wrote on last edited by
    #1

    Dear Experts, I want to define a global variable for Database Connection variable in C# that can be accessed from any procedures in Project. I see this in VB but in C# I cant do it. Your help is highly appreciated. Regards,

    OriginalGriffO A 2 Replies Last reply
    0
    • A aahamdan

      Dear Experts, I want to define a global variable for Database Connection variable in C# that can be accessed from any procedures in Project. I see this in VB but in C# I cant do it. Your help is highly appreciated. Regards,

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      C# doesn't have global variables: the closest you can come is to create a static property of a Database class and use that:

      class Database
      {
      public static SqlConnection Connection { get; private set; }
      public void Create(string strConnect)
      {
      Connection = new SqlConnection(strConnect);
      Connection.Open();
      }
      }

      But personally, I wouldn't do that - it encourages you to maintain a connection for teh life of the application which is a pretty poor practice. It also means that you only get one connection, so some operations are not possible - any INSERT or UPDATE while processing a SqlDataReader for example. I'd create the connections as I need them, inside a using block so that they are closed and disposed when I'm done.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • A aahamdan

        Dear Experts, I want to define a global variable for Database Connection variable in C# that can be accessed from any procedures in Project. I see this in VB but in C# I cant do it. Your help is highly appreciated. Regards,

        A Offline
        A Offline
        Afzaal Ahmad Zeeshan
        wrote on last edited by
        #3

        You never should create a variable for database connection in a global scope. It would consume resources even where you do not need a database connection. Such as, when user is not authenticated, and you're showing him a view (or a dialog box) to tell him that he cannot connect to the database for query purposes, but in the background database would be connected already. Which won't be a good usage of your logic or resources. It would be better to create this variable, inside the function where you need it. Connection pooling would take care of the underlying connection maintenance for your application so that you won't get a delay for connecting to the database server.

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        A 1 Reply Last reply
        0
        • A Afzaal Ahmad Zeeshan

          You never should create a variable for database connection in a global scope. It would consume resources even where you do not need a database connection. Such as, when user is not authenticated, and you're showing him a view (or a dialog box) to tell him that he cannot connect to the database for query purposes, but in the background database would be connected already. Which won't be a good usage of your logic or resources. It would be better to create this variable, inside the function where you need it. Connection pooling would take care of the underlying connection maintenance for your application so that you won't get a delay for connecting to the database server.

          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

          A Offline
          A Offline
          aahamdan
          wrote on last edited by
          #4

          Thank you Sir, good notes. Appreciated your support

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            C# doesn't have global variables: the closest you can come is to create a static property of a Database class and use that:

            class Database
            {
            public static SqlConnection Connection { get; private set; }
            public void Create(string strConnect)
            {
            Connection = new SqlConnection(strConnect);
            Connection.Open();
            }
            }

            But personally, I wouldn't do that - it encourages you to maintain a connection for teh life of the application which is a pretty poor practice. It also means that you only get one connection, so some operations are not possible - any INSERT or UPDATE while processing a SqlDataReader for example. I'd create the connections as I need them, inside a using block so that they are closed and disposed when I'm done.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            A Offline
            A Offline
            aahamdan
            wrote on last edited by
            #5

            Thank you Sir.

            OriginalGriffO 1 Reply Last reply
            0
            • A aahamdan

              Thank you Sir.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You're welcome!

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              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