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

I have two connection string. one is server, another one is Local.

Whenever server connection fails , System has to work with local connection.


C#
public  static string fncGetConnection()
      {

          string sCurrentConnection = "";
          try
          {
              using (SqlConnection con = new SqlConnection(Server))
              {

                  con.Open();

                  sCurrentConnection = Server;
              }
          }
          catch (SqlException ex)
          {
              using (SqlConnection con = new SqlConnection(Local))
              {
                  con.Open();
                  sCurrentConnection = Local;
              }
          }

          return sCurrentConnection;

      }



I used above function to get the connection string. but it takes more time around(50 secs) to check the first connection string fails. how to speed up checking connection and using application with local connection?


Regards,
Pal.
Posted

First time connection will always take long.. Creating a connection is expensive; it requires your application to connect to the database server, authenticate, and return a valid connection.

Subsequently connection takes less time as it caches some information.. but in your case the connection is a failure so every time you connect it will take the same time..

1. You could try connect during loading the application using a separate thread.. You will know whether the connection valid or not..

2. Somwhere before the user uses the connection you could do a quick check on the connection..

connection to a failed connection will always take time and you may not able to improve it that much..
 
Share this answer
 
That's caused by the ConnectionTimeout value. You can change that in your connection string, e.g. by appending ;Connection Timeout=10
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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