如何限制SqlConnection.Open()的等待时间?

发布于 2024-08-12 06:54:54 字数 215 浏览 2 评论 0原文

我想测试连接字符串是否正确,因此我创建了一个新的 SqlConnection,调用其 Open() 方法。但是当连接字符串的 server/data_source 部分错误时,我必须等待很长时间才能返回。

我尝试在连接字符串中添加连接超时,但没有成功; 我尝试在另一个线程中打开连接,然后几秒钟后调用 Thread.Abort() 。 他们都没有工作。

那么正确的方法是什么? 谢谢。

I want to test if the connection string is correct, so I created a new SqlConnection, called its Open() method. But I have to wait a long time before it returns when the server/data_source part of connection string is wrong.

I tried adding connection timeout to the connection string, it didn't work;
I tried open the connection in another thread, then I call Thread.Abort() after several seconds.
None of them worked.

So what's the correct way to do this?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-08-19 06:54:54

经过研究,我找到了解决方案( http://www. Improve.dk/blog/2008/03/10/controlling-sqlconnection-timeouts ),我稍微修改了一下。如果没有抛出异常,则连接字符串有效。

        var alive = true;
        string error = null;
        var success = false;

        // ReSharper disable AccessToModifiedClosure
        // ReSharper disable UseObjectOrCollectionInitializer
        var thread = new Thread(() =>
                                    {
                                        try
                                        {
                                            var connection = new SqlConnection(connectionString);
                                            connection.Open();
                                            connection.Close();

                                            if (alive)
                                                success = true;
                                        }
                                        catch (SqlException ex)
                                        {
                                            if (alive)
                                                error = ex.Message;
                                        }
                                        catch (ThreadAbortException)
                                        {
                                        }
                                        finally
                                        {
                                            if (connection.State == ConnectionState.Open)
                                                connection.Close();
                                        }
                                    });
        // ReSharper restore AccessToModifiedClosure
        // ReSharper restore UseObjectOrCollectionInitializer
        thread.IsBackground = true;
        var sw = Stopwatch.StartNew();
        thread.Start();

        var timeout = TimeSpan.FromSeconds(3);
        while (sw.Elapsed < timeout)
            thread.Join(TimeSpan.FromMilliseconds(200));
        sw.Stop();

        if (!success)
        {
            alive = false;
            throw new Exception(error ?? "Connection timeout, please check the connection string.");
        }

after researching, I found the solution ( http://www.improve.dk/blog/2008/03/10/controlling-sqlconnection-timeouts ), I modified it a little bit. If there's not exception thrown, then the connection string is valid.

        var alive = true;
        string error = null;
        var success = false;

        // ReSharper disable AccessToModifiedClosure
        // ReSharper disable UseObjectOrCollectionInitializer
        var thread = new Thread(() =>
                                    {
                                        try
                                        {
                                            var connection = new SqlConnection(connectionString);
                                            connection.Open();
                                            connection.Close();

                                            if (alive)
                                                success = true;
                                        }
                                        catch (SqlException ex)
                                        {
                                            if (alive)
                                                error = ex.Message;
                                        }
                                        catch (ThreadAbortException)
                                        {
                                        }
                                        finally
                                        {
                                            if (connection.State == ConnectionState.Open)
                                                connection.Close();
                                        }
                                    });
        // ReSharper restore AccessToModifiedClosure
        // ReSharper restore UseObjectOrCollectionInitializer
        thread.IsBackground = true;
        var sw = Stopwatch.StartNew();
        thread.Start();

        var timeout = TimeSpan.FromSeconds(3);
        while (sw.Elapsed < timeout)
            thread.Join(TimeSpan.FromMilliseconds(200));
        sw.Stop();

        if (!success)
        {
            alive = false;
            throw new Exception(error ?? "Connection timeout, please check the connection string.");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文