Dynamic Connections to variable databases in c sharp

Say we have Four different Databases and their connections as well which are stored in configuration file like web.Config or

<App-Name>.config and we are in need to connect to different database depending upon user type


User ID=admin;Initial Catalog=biblio;Data Source=betav9;
Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;
Workstation ID=BETAV9;Use Encryption for Data=False;
Tag with column collation when possible=False"/>

When we have taken a decision over user type, we may pass a value to if else structure or case. Depending upon the value you can get the type of connection to
a database we want.

Object dbCon = null;

switch(userType)
{

case "sqlConnection"
dbCon = new SqlConnection("SqlServerConString");
// provide appropriate sql command
break;

case "OleDbConnection"
dbCon = new System.Data.OleDb.OleDbConnection("OleDbConString");
// provide appropriate sql command
break;

case "OdbcConnection"
dbCon = new OdbcConnection("OdbcCongString");
// provide appropriate sql command
break;

case "OracleConnection"
dbCon = new OleDbConnection ("OracleConString");
// provide appropriate sql command
break;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s