Results 1 to 4 of 4

Thread: Complete Newbie - Global Variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    16

    Question Complete Newbie - Global Variable

    Ok I want to make a connection to a database. I can do that but it is obviously only in the scope of the function i have created.

    Is there a way to leave that connection open perminantly so that other functions can call upon it.

    Remember complete newbie here. Any help is greatly appreciated.

    aaron

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Complete Newbie - Global Variable

    You could do that but you shouldn't. ADO.NET was specifically designed NOT to work that way. You should create, open, close and destroy database connections on an "as needed" basis. You would store your connection string, or the components of your connection string, somewhere common so that it could be used by all connections.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    16

    Re: Complete Newbie - Global Variable

    The reason I wanted to do it this way is that I will be connecting to a remote database and will be having very frequent data checks, like at times every 5 seconds. I need to recreate the feel of live updates to a screen with new data that is being deliveried from other users on the system.

    Would there be a better way of going about it?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Complete Newbie - Global Variable

    I think I may have misread your original post. because of the term "global" I was assuming that you wanted the one connection available throughout the app, which is what "global" usually means. In your case it's just a member variable you want. A local variable is declared within a specific method so its only available within that method. A member variable is declared within the type but outside all methods, so it's accessible from any method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width