Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All.

I m working on a windows application.
it ll be used in two locations.
sql server ll b at one location.
so i hv given two connection strings in app-config file with two different names. (Local and Web)

now how to check which connection is available?


for connection i hv written following code in a class.

C#
public sealed class Connection : MyDBManager
   {
       private static string _dbUser, _dbPwd, _dataSource, _dataProvider;
       public string DBUser { get { return _dbUser; } set { _dbUser = value; } }
       public string DBPwd { get { return _dbPwd; } set { _dbPwd = value; } }
       public string DBSource { get { return _dataSource; } set { _dataSource = value; } }
       public string Provider { get { return _dataProvider; } set { _dataProvider = value; } }


       public Connection()
       {
           ConnectToDB();
       }

       public Connection(string ServerName)
       {
           ConnectToDB(ServerName);
       }

       private bool ConnectToDB()
       {
           getDBValues();
           string connStr = ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString;
           base.ConnectionString = connStr;
           try
           {
               base.Open();
               return true;
           }
           catch
           {
               return false;
           }
       }

       private bool ConnectToDB(string ServerName)
       {
           if (ServerName == "DelhiServer")
           {
               getDBValues();
               string connStr = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
               base.ConnectionString = connStr;
               try
               {
                   base.Open();
                   return true;
               }
               catch
               {
                   return false;
               }
           }
           else
           {
               return false;
           }
       }

       public bool DisposeConnection()
       {
           base.RemoveParameters();
           base.Close();
           base.Dispose();
           return true;
       }

       private void getDBValues()
       {
           //Cryptograph crypt= new Cryptograph();
           //string _dataProvider;
           //_dbUser = crypt.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["dbUser"],PW_KEY);
           //_dbPwd = crypt.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["dbPwd"], PW_KEY);
           //_dataSource = crypt.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["dbSource"], PW_KEY);
           //_dataProvider=System.Configuration.ConfigurationSettings.AppSettings["dataProvider"];

           _dataProvider = "SQLSERVER";
           if (_dataProvider.ToUpper() == "OLEDB")
           {
               base.ProviderType = DataProvider.OleDb;
           }
           else if (_dataProvider.ToUpper() == "ORACLE")
               base.ProviderType = DataProvider.Oracle ;
           else if (_dataProvider.ToUpper() == "SQLSERVER")
               base.ProviderType = DataProvider.SqlServer;

           else if (_dataProvider.ToUpper() == "ODBC")
               base.ProviderType = DataProvider.Odbc;
           else
               base.ProviderType = DataProvider.OleDb;

       }
   }



please kindly help me to do this.

thanks in advance
Posted

This might not be the most elegant or efficient way to do it, but it does work, This code first tries connecting with windows authentication and if that fails it will try SQL authentication. In essence it works on the same principal as your question.
All you will need to do is use the two different connection strings at different stages within the try catches.

C#
public void Connect()
       {
           try
           {
               string connectionString = "Data Source='" + ServerAdrress.Text + "';Initial Catalog='" + DatabaseSelection.SelectedItem.ToString() + "';Integrated Security=SSPI;";
               string SQLconnectionString = "Data Source='" + ServerAdrress.Text + "';Initial Catalog='" + DatabaseSelection.SelectedItem.ToString() + "';Persist Security Info=True;User ID='" + ConnectionUser.SelectedItem.ToString() + "';Password='" + lblPassword.Text + "'";
               try
               {
                   conn = new SqlConnection(connectionString);
                   conn.Open();
               }
               catch (Exception ex)
               {
                   try
                   {
                       conn = new SqlConnection(SQLconnectionString);
                       conn.Open();
                   }
                   catch (Exception)
                   {
                       MessageBox.Show(ex.Message);
                       conn.Close();
                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }


Hope this helps.
 
Share this answer
 
v2
Comments
dibyaaryan007 26-Oct-13 0:08am    
i have tried it but its taking too much time to check. :(
You only need one connection string, simply distribute different config files to each location.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900