This article describes the method of using Socket to quickly determine whether a database connection is normal. Share it for your reference. The specific analysis is as follows:
When everyone is working on projects, they are usually related to databases. I believe that many people do not determine whether the database connection is successful when opening the database with code. If you write it correctly, it's okay, but if you make an error, you will wait for a long time (the default is 15s, but the actual response may be longer). And it will also cause a stuck situation, which will make the user feel very bad. Therefore, it is particularly important to quickly determine whether the database connection is successful.
Generally speaking, if the connection string is correct, the time it takes to connect to the database is generally within 5 seconds. So we can add a waiting time at the end of the connection string: timeout. like:
If the connection is not successful, a response should be made within 5-8s. But it is not certain that there must be an error in the connection string. If a large number of people visit, the waiting time is likely to occur for more than 5 seconds. Moreover, if the connection is not successful, we still have to wait at least 5 seconds. The feeling of waiting is not good, the user will definitely give you a fork, so we need to use other methods to make quick judgments.
Here we use Socket to implement this function. (As for what Socket is, friends who are interested can check the information themselves)
First use Socket to determine whether the connection to the server is successful, and then perform database operations.
#region uses Socket to test server connection/// <summary> /// Use Socket method to test the server connection/// </summary> /// <param name="host">Server hostname or IP</param>/// <param name="port">port number</param>/// <param name="millisecondsTimeout">Waiting time: milliseconds</param>/// <returns></returns> public static bool TestConnection(string host, int port, int millisecondsTimeout) { TcpClient client = new TcpClient(); try { var ar = (host, port, null, null); (millisecondsTimeout); return ; } catch (Exception e) { throw e; } finally { (); } } #endregion
The following are the database operations, which can be modified according to your own situation.
/// <summary> /// The database connection operation can be replaced with your own program/// </summary> /// <param name="ConnectionString">Connection String</param>/// <returns></returns> private static bool TestConnection(string ConnectionString) { bool result = true; try { SqlConnection m_myConnection = new SqlConnection(ConnectionString); m_myConnection.Open(); //Database operation... m_myConnection.Close(); } catch (Exception ex) { (()); result = false; } return result; }
I used winForm to test it here.
private void btnSocket_Click(object sender, EventArgs e) { string strCon = "Data Source=192.168.24.566;Initial Catalog=qmaster;User ID=sa;password=123456"; string[] s=(';'); s = s[0].Split('='); //Get IP string strIP =s[1]; //Send data to determine whether to connect to the specified ip if (TestConnection(strIP , 1433, 500)) { //Connection is successful ("Socket Link Succeed","Connect the server"); // For database operation, I used connection test here. Can be modified according to your system if (TestConnection(strCon)) ("Sql Link Succeed","Connect the database"); else ("Sql Link Failed", "Connect the database"); } else ("Socket Link Failed","Connect the server"); }
Using Socket to test whether to connect to the server is very convenient and very fast, so you don’t have to wait for that long. And you can also know whether the server address is wrong or the database is wrong. After testing, if the database address is wrong, the results can be feedback within 1-3s. Prompt the user "Socket Link Failed". If the database name is wrong, it will only prompt "Sql Link Failed".
Will you miss such a convenient, fast, efficient and easy to troubleshoot? I don’t know, I won’t miss it anyway!
I hope this article will be helpful to everyone's C# programming.