I have the following code in the global.asax file (in Session on start):
ConnectionStringSettings settings1 = ConfigurationManager.ConnectionStrings["...
OleDbConnection conn = new OleDbConnection(settings1.ToString());
____
I am getting an error when attempting to use the Open command on this string:
OleDbCommand myCmd = new OleDbCommand(strSQL, conn)
The error is:
The name 'conn' does not exist within the current context.
What is the best way to reference a database connection string in C#? Need to access a db on various pages.?
The global in Global.asax refers to code that is globally available not variables whose contents are globally available.
What I usually do is to follow the Factory pattern, and create a Class which has static methods that will return a connection (I am typing freehand, so you may need to change case to get this to compile):
public class DBFactory
{
public static OleDbConnection GetConnection()
{
return new OleDbConnection(settings.ToString());
}
}
Now, whenever you use it, you just call the factory:
OleDbConnection conn = DBFactory.GetConnection;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment