c# - Using context connection=true for SqlConnection -


i have simplified class:

public abstract class basedaservice {     private readonly string _connectionstring;      protected basedaservice(string connectionstring)     {         _connectionstring = connectionstring;         }          protected idbconnection openconnection()     {         idbconnection connection = new sqlconnection(_connectionstring);         connection.open();         return connection;         } } 

here connection string comes config file. use credentials config files whilst doing:

new sqlconnection("context connection=true")

can adapt connection string in config file or manipulate 'connection' instance of idbconnection achieve this?

i suggest use sqlconnectionstringbuilder class

 // default parameter set false can still use  // same code before , change spots  // context required  protected idbconnection openconnection(bool usecontext = false)  {      string newconstring = _connectionstring;     if(usecontext)     {         sqlconnectionstringbuilder scs = new sqlconnectionstringbuilder(_connectionstring);         scs.contextconnection = true;         newconstring = scs.tostring();     }     idbconnection connection = new sqlconnection(newconstring);     connection.open();     return connection } 

of course changing app.config matter of preference.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -