连接超时未得到遵守

时间:2014-10-08 20:08:45

标签: sql-server c#-4.0

我想使用短连接超时来记录到sql server数据库并测试我正在尝试连接到不存在的数据库服务器/ IP并且我期望在连接字符串中设置的值之后,它应该返回,但这不会发生。

    static void Main(string[] args)
    {
        string connectionString = "Data Source=<DBServerThatDoesNotExist>;Initial Catalog=LogDB;Connect Timeout=3; User ID=XXX; Password=XXX";
        using (SqlConnection conn = new SqlConnection(connectionString))
        {

            Console.Out.WriteLine("connection timeout:" + conn.ConnectionTimeout);
           // This correctly prints 3 seconds...
            Stopwatch sw = new Stopwatch();
            try
            {
                sw.Start();
                conn.Open();
            }
            catch (Exception)
            {

            }
            finally
            {
                sw.Stop();
                var time = sw.Elapsed.Seconds;
                Console.Out.WriteLine(time);
                // this time is way to high like anywhere between 21 to 50 seconds...
            }
        }
    }

我无法理解为什么我的连接超时不受尊重。有什么猜测吗?

0 个答案:

没有答案