Global variable in C#
-
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,
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...
-
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,
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 !~
-
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 !~
-
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...
-
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...