我正在将我的应用程序与远程数据库 (SQL Server 2012) 连接,我遇到了“连接超时已过期”问题。尝试使用登录前握手确认时超时期限已过。这可能是因为登录前握手失败或服务器无法及时响应。尝试连接到此服务器时花费的时间是 - [登录前] 初始化 = 211;握手 = 14787;"
需要注意的是,应用程序在远程数据库上运行良好,但此错误一天只发生几次(例如,一天 100 次尝试中有 5 次),其余所有应用程序在该数据库上运行良好。
下面给出了连接SQL Server数据库并执行查询的C#代码。
List<string> lstDuplicateSNames = new List<string>();
string commaSeparatedStudentNames = "X,Y,Z,A,B,C";
string query = "SELECT DISTINCT(Student_NAME) FROM V_STUDENT_RECORDS WHERE Student_NAME IN ('" + commaSeparatedStudentNames + "')";
using (SqlConnection myConnection = new SqlConnection("Database=Student_DB;Server=nkj-connect.com;Integrated Security=true;"))
{
using (SqlCommand myCommand = new SqlCommand())
{
myCommand.Connection = myConnection;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = query;
myCommand.CommandTimeout = 0;
myCommand.Connection.Open();
using (SqlDataReader reader = myCommand.ExecuteReader())
{
// Check is the reader has any rows at all before starting to read.
if (reader.HasRows)
{
while (reader.Read())
{
lstDuplicateSNames.Add(reader.GetString(0));
}
}
}
}
}
请提出我在这里缺少的内容。
最佳答案
我遇到过类似的情况。对我有用的是将连接服务器从服务器名称更改为 IPAddress,在您的情况下 nkj-connect.com
关于c# - 尝试使用登录前握手确认时超时期限已过。 (一天只有几次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44023137/