Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何减少打开连接的等待时间?_C#_Database Connection - Fatal编程技术网

C# 如何减少打开连接的等待时间?

C# 如何减少打开连接的等待时间?,c#,database-connection,C#,Database Connection,我检查连接是否可用,代码为: SqlConnection objConnection = new SqlConnection(string.Concat(connectionString)); try { objConnection.Open(); // this line make wait time if connection not available objConnection.Close();

我检查连接是否可用,代码为:

 SqlConnection objConnection = new SqlConnection(string.Concat(connectionString));
        try
        {
            objConnection.Open(); // this line make wait time if connection not available 
            objConnection.Close();
            SqlConnection.ClearAllPools();
            return true;
        }
        catch
        {
            return false;
        }

当连接不可用时,需要很长时间才能回答。如何减少连接?

您可以通过这种方式检查sql连接,这不会花费很长时间

        SqlConnection objConnection = new SqlConnection(string.Concat(connectionString));
        try
        {
           objConnection.Open();
        }
        catch {}

        if (objConnection != null && objConnection.State == ConnectionState.Open)
        {
            try
            {
               objConnection.Close();
            }
            catch {}
            return true;
        }
        else if (objConnection != null && objConnection.State == ConnectionState.Closed)
        {
            try
            {
               objConnection.Close();
            }
            catch {}
            return false;
        }

您可以通过这种方式检查sql连接,这不会花费很长时间

        SqlConnection objConnection = new SqlConnection(string.Concat(connectionString));
        try
        {
           objConnection.Open();
        }
        catch {}

        if (objConnection != null && objConnection.State == ConnectionState.Open)
        {
            try
            {
               objConnection.Close();
            }
            catch {}
            return true;
        }
        else if (objConnection != null && objConnection.State == ConnectionState.Closed)
        {
            try
            {
               objConnection.Close();
            }
            catch {}
            return false;
        }

如果要连接到SQL Server,可以先尝试连接服务器。在我的盒子上,ping只需要5秒钟就可以得出服务器无法访问的结论。此代码段中显示了利用该功能的最简单代码:

if(new System.Net.NetworkInformation.Ping().Send("Your servername here").Status != 
       System.Net.NetworkInformation.IPStatus.TimedOut)
 {
    // server reachable, try a real SQL Server connection now
    SqlConnection objConnection = new SqlConnection(connectionstring);
   try
   {
       objConnection.Open(); // this line make wait time if connection not available 
       objConnection.Close();
       // not sure why you would want this
       // only use if you want worse performance
       // SqlConnection.ClearAllPools();
      return true; 
   }
   catch
   {
       return false; 
   }
}
else 
{
    return false; // PING failed
}

系统管理员可能会禁用/阻止ICMP通信,因此此选项可能不适用于每台服务器。

如果要连接到SQL server,可以先尝试连接服务器。在我的盒子上,ping只需要5秒钟就可以得出服务器无法访问的结论。此代码段中显示了利用该功能的最简单代码:

if(new System.Net.NetworkInformation.Ping().Send("Your servername here").Status != 
       System.Net.NetworkInformation.IPStatus.TimedOut)
 {
    // server reachable, try a real SQL Server connection now
    SqlConnection objConnection = new SqlConnection(connectionstring);
   try
   {
       objConnection.Open(); // this line make wait time if connection not available 
       objConnection.Close();
       // not sure why you would want this
       // only use if you want worse performance
       // SqlConnection.ClearAllPools();
      return true; 
   }
   catch
   {
       return false; 
   }
}
else 
{
    return false; // PING failed
}

系统管理员可能会禁用/阻止ICMP通信,因此此选项可能不适用于每台服务器。

请勿
清除所有池
!连接池专门用于提高连接效率。当您
Open()
时,将使用池中的连接(如果有)。当您
Close()
时,它会返回到池中,但不会被销毁,只是等待有人再次使用它。

不要
clearlallpools
!连接池专门用于提高连接效率。当您
Open()
时,将使用池中的连接(如果有)。当您
Close()
时,它会返回到池中,但不会被销毁,只是等待有人再次使用它。

如果您通过TCP协议连接,您可以尝试服务器是否配置为响应简单的ping,并在启动真正的TCP连接之前使用它。我假设您的客户端有网络连接。我没有访问配置服务器的权限。如果您通过TCP协议连接,您可以尝试将服务器配置为响应简单的ping,并在启动真正的TCP连接之前使用它。我假设您的客户端有网络连接。我没有访问配置服务器的权限。如果不打开,您没有选择检查,如果您尝试打开它但失败,则肯定需要时间,因为它将尝试重新连接,然后报告您它无法打开。如何减少此时间(它将尝试重新连接)?有可能吗?请查看我的最新更新并仔细阅读这个有用的解释问题@MostafizurRahman,您最好在返回之前关闭此连接。如果没有打开,您将别无选择地进行检查,如果您尝试打开它但失败,则肯定需要时间,因为它将尝试重新连接,然后报告您它无法打开。如何减少此时间(它将尝试重新连接)?有可能吗?请查看我的最新更新并仔细阅读这个有用的解释问题@MostafizurRahman,您最好在返回之前关闭此连接。