The setting of Connect Timeout in the SQL connection string is invalid and can only be determined through Socket.
The Code is as follows:
/// <Summary>
/// Test whether the database server is available
/// </Summary>
/// <Param name = "ip"> Server IP </param>
/// <Param name = "port"> server port, usually 1433 </param>
/// <Param name = "timemout"> set the timeout time in milliseconds </param>
/// <Returns> If the database is available, true is returned; otherwise, false is returned. </returns>
Static bool TestConn (string ip, int port, int timemout)
{
Using (TcpClient client = new TcpClient ())
{
ManualResetEvent mre = new ManualResetEvent (false );
Client. BeginConnect (ip, port, delegate (IAsyncResult res)
{
If (client. Connected)
{
Mre. Set ();
Console. WriteLine ("123 ");
}
}, Null );
ThreadPool. QueueUserWorkItem (delegate (object obj) {Thread. Sleep (timemout); mre. Set ();});
Mre. WaitOne ();
If (! Client. Connected)
{
Return false;
}
Return true;
}
}
Test code:
DateTime begin = DateTime. Now;
Console. WriteLine (TestConn ("192.168.1.99", 1433,200 0 ));
Console. WriteLine (DateTime. Now. Subtract (begin). TotalSeconds );