自动连接SQLConnection OpenAsync()队列? [英] Auto Connect SQLConnection OpenAsync() Queue up?

查看:302
本文介绍了自动连接SQLConnection OpenAsync()队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个等待表单,当sql连接不可用时出现(例如服务器关闭/局域网禁用)等待表单将一直显示,直到连接再次可用,所以我不会需要继续执行查询以检查连接是否可用。

这是我的工作,



I'm trying to create a wait form that will appear when sql connection is not available (ex. server shutted down / lan network disabled) the wait form will keep appearing until the connection is available again, so I don't need to keep executing query to check if the connection is available or not.
Here is what I do,

private void SQLClientLoader_Load(object sender, EventArgs e)
        {
            this.GetSQLState();
        }




async private System.Threading.Tasks.Task<ConnectionState> GetStateAsync()
        {
            do
            {
                this.cts = new CancellationTokenSource();
                this.cnn = new SqlConnection(this.par.Constr);
 
                try
                {
                    await Task.Delay(2500);
 
                    await this.cnn.OpenAsync(cts.Token);
                }
                catch (Exception ex)
                {
                    cts.Cancel(false);
 
                    this.cnn.Dispose();
                    this.cts.Dispose();
                }
            } while (this.cnn.State != ConnectionState.Open);
 
            return this.cnn.State;
        }




async private void GetSQLState()
        {
            
 
            if (await GetStateAsync() == ConnectionState.Open)
            {
                this.Close();
            }
 
            await Task.Delay(2500);
        }





此代码正常运行,但如果等待表单循环超过5次,则需要一段时间它关闭了。

服务器不可用的时间越长,表单在关闭之前等待的时间越长。

所以当连接可用时我试图将断点放在循环中,它无法打开连接,就像cnn.OpenAsync()队列一样。

我的代码有什么问题吗?



注意:我停止并启动sql实例服务以尝试此代码。



This code is working, but if the wait form looped for more than 5 times, it takes a while before it close itself.
The longer the server unavailable, the longer the form will wait before it closes.
So I tried to put breakpoint inside the loop when the connection is available, it can't open the connection, it's like the cnn.OpenAsync() queue up.
Anything wrong in my code?

Note : I stop and start the sql instance service to try this code.

推荐答案

所以是的,我终于在2周后找到了解决方案。

我只需要清除特定的连接池。

然后一切顺利,就像我哇哇哇哇哇哇哇哇。
So yes I finally found the solution after 2 weeks.
I just need to clear the specific connection pool.
Then everything run as smooth as oh wow made my day.


这篇关于自动连接SQLConnection OpenAsync()队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆