Umang Agrawal
Umang Agrawal

Reputation: 515

does declaring a variable in namespace makes it global

i am writing a windows form based application that uses database and i want to declare the following as global so that i can just use vcon.open() and vcon.close() to open and close my database from which ever form i want. please tell how to do it.

OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=F:\workspace\RDASMS\RDASMS\rdadb.mdb");

Upvotes: 1

Views: 1957

Answers (4)

Pete Garafano
Pete Garafano

Reputation: 4913

You can create a static class with static members.

public static MyConnection
{
    public static OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\workspace\RDASMS\RDASMS\rdadb.mdb");
}

Then you can access it like:

var vcon = MyConnection.Connection;
vcon.Open();

Doing it this way adds a unnecessary layer of complexity to your program. You should either create a class that strictly handles database access such as the MyConnection example above. You would need to add the appropriate methods to actually handle the access. Using the Jet provider can introduce significant performance implications when opening and closing a file constantly. You should only close it if there will be long periods of inactivity on the connection. In that case you should do something more along the lines of:

using(var vcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\workspace\RDASMS\RDASMS\rdadb.mdb"))
{
    // Your code here...
}

This will close the connection once your operations are complete. But don't forget about the performance issues that can arise from this.

Upvotes: 0

Inisheer
Inisheer

Reputation: 20794

As the others have stated, "global" static types are not usually the best design approach, but to answer your specific question:

namespace YourApplicationNamespace
{
    public static class MyOleDbConnection
    {
        public static OleDbConnection OleDbConnection;

        public static void Open()
        {
             // Do Something with OleDbConnection.
        }

        public static void Close()
        {
             // Do Something with OleDbConnection.
        }
    }
}

Can be used as:

MyOleDbConnection.Open();
MyOleDbConnection.Close();

Upvotes: 1

Felice Pollano
Felice Pollano

Reputation: 33252

First of all you can't declare something global in a namespace. And even if it was possible it is not a nice idea. Appreciating your effort in avoiding code duplication, I suggest you to:

  1. Place the connection string in the configuration.
  2. Declare the connection and use it when needed, maybe with an using pattern.

a point about 2: I noticed you use a jet engine ( Access ) this can result in a performance drawback by opening and closing the connection on demand, or at least was as this in the past. In any case note than opening the connection when needed and close it as soon you have done is the pattern to follow in any other case.

Upvotes: 0

Drew Noakes
Drew Noakes

Reputation: 310917

Connections are quite lightweight, so you can just create and close them as needed. Maybe like this:

using (var conn = new OleDbConnection(connStr))
{
    // Use your connection
}

This will automatically close the connection for you when the block exits.

You might like to put the string in a globally accessible place, such as a Config class.

Upvotes: 1

Related Questions