c# - 尝试使用登录前握手确认时超时期限已过。 (一天只有几次)

标签 c# sql-server connection-timeout

我正在将我的应用程序与远程数据库 (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/

相关文章:

c# - 如何使用依赖注入(inject)从多个来源获取配置?

c# - 无法加载文件或程序集 'VSLangProj80'

c# - Dapper async/await 连接超时

azure - Windows Azure 网站请求超时

c# - 如何以编程方式将文本转换为 prc/mobi 文件?

c# - 在具有大量流量的异步 ASP.NET Web API 中,将 ConfigureAwait 设置为 false 会产生什么影响?

c# - 从 DateTimePicker 中删除日期和月份之前的空格

sql-server - hibernate 锁表

sql-server - 在 SQL Server 中编写架构脚本

c# - Mysql 备份超时过期 - C#