脱机-联机更改 [英] Offline - Online Connection Changing

查看:76
本文介绍了脱机-联机更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两个连接字符串.一个是服务器,另一个是本地.

每当服务器连接失败时,系统都必须使用本地连接.


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.


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;

      }




我使用上面的函数来获取连接字符串.但是要花更多的时间(50秒)来检查第一个连接字符串是否失败.如何加快检查连接以及将应用程序与本地连接一起使用?


问候,
Pal.




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.

推荐答案

第一次连接将总是花费很长时间.它要求您的应用程序连接到数据库服务器,进行身份验证并返回有效的连接.

随后的连接会花费较少的时间,因为它会缓存一些信息..但是在您的情况下,连接失败,因此每次连接都将花费相同的时间..

1.您可以在加载应用程序时使用单独的线程尝试连接..您将知道连接是否有效..

2.在用户使用连接之前,可以先快速检查一下连接..

连接到失败的连接将始终花费时间,您可能无法对其进行太多改进..
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..


这是由ConnectionTimeout值引起的.您可以在连接字符串中进行更改,例如通过附加;Connection Timeout=10
That''s caused by the ConnectionTimeout value. You can change that in your connection string, e.g. by appending ;Connection Timeout=10


这篇关于脱机-联机更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆