Connection String best practice?

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 28-Nov-2007 18:31:25   

With the Adapter, what is the best practice to set a connection string once when it can not be set in the app.config (company policy)

In SelfService I just call the DBUtils.ActualConnectionString once in the init of the app.

What is the best way to set it once on app startup in the Adapter model?

fpw23
User
Posts: 12
Joined: 16-Dec-2005
# Posted on: 28-Nov-2007 19:58:12   

I created a static helper class to do it:



public class HelperDataAccess
{
    private static string _activeConnectionString;

                /// <summary>
    /// The connection string to use when connecting to the database
    /// </summary>
    public static string ActiveConnectionString
    {
        get
        {
            return _activeConnectionString;
        }
        set
        {
            _activeConnectionString = value;
        }
    }

    /// <summary>
    /// Common place to get a data adapter object.
    /// </summary>
    /// <returns>the correct IDataAccessAdapter</returns>
    public static IDataAccessAdapter GetDataAdapter()
    {
        IDataAccessAdapter da = new DataAdapterMSSQL(HelperDataAccess.ActiveConnectionString);
            ((DataAccessAdapter)da).CatalogNameUsageSetting = CatalogNameUsage.Clear;
            return da;
        }
}

at app load i set the active connection string property which is static and should never change and then call the getdataadapter method which creates the adapter with the correct connection string, I also have mine own dataadapter I use so i can trap save and delete events but all you would need to do is return the regular adapter