Hi all,
Not sure if this is specifically an ADO problem, but I have an application which the user logs into SQL server through.
They then manipulate objects at will which relate to the database.
I have the problem that I wish to have a single DB connection for the whole application. In VB6 this was relatively simple - just have a public copy of an ADO connection object in a module.
However C# being object-oriented doesn't allow this (or does it?). Ideally I would like to access the ODBC connection from a single point - eg a static class. Is there a way to make a static variable persist once its class is accessed so that every time I want to save an object in the data access layer back to the DB, a new DB connection isn't created - ie only one for the whole app??
What Im after really is somthing like (pseudo-code)
Code:
namespace DBAccess{
public static class DBConnection
{
private ODBCConnection objConnection;
public static ODBCConnection DBAccess(string username, string password)
{
if(objConnection == null){
objConnection = new ODBCConnection;
try{
// get user login details
}catch(..)
{}
}
return objConnection; // I want this instance to persist
}
}
}
then I simply want to be able to call the following at any point in the project.
The first call will instantiate the DB connection, and get the user to login.
using DBAccess;
public class someclass1
{
private ODBCConnection objConn = DBConnection.DBAccess;
}
public class someclass2
{
private ODBCConnection objConn = DBConnection.DBAccess;
}
Then the above two classes effectively share the same DB connection
Is this possible???
I've seen one post where the connection string is persisted to a config file - but this is neither secure unless I use some cryptography, and it requires a new connection for every access. Not what I want.
If I'm talking rubbish then please dont be mean - im new to c#.
Thanks
Dave