【问题标题】:Show state of MySqlConnection in Windows Forms在 Windows 窗体中显示 MySqlConnection 的状态
【发布时间】:2021-06-20 23:18:36
【问题描述】:

我使用 C# 创建了一个 Windows 窗体应用程序,用于检查 MySQL 连接是打开还是关闭。对于每个状态,标签的文本都应该改变。

我的问题是:如果应用程序继续运行并且连接关闭或终止,标签的文本不会改变。

我尝试使用 form_load 事件,但没有成功。

这是我的代码:

string connectionString = "Data Source=localhost;Initial Catalog=table;User ID=root;Password=";

public Form1() 
{          
    InitializeComponent();           
    MySqlConnection cnn = new MySqlConnection(connectionString);

    try
    {
        cnn.Open();
        toolStripStatusLabel1.BackColor = System.Drawing.Color.Green;
        toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
        toolStripStatusLabel1.Text = "Connection Success";
    }
    catch (Exception ex)
    {
        toolStripStatusLabel1.BackColor = System.Drawing.Color.Red;
        toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
        toolStripStatusLabel1.Text = "Connection failed!";
    }
    finally
    {
        cnn.Close();
    }
}

【问题讨论】:

  • 订阅 StateChange 事件 dev.mysql.com/doc/dev/connector-net/8.0/html/… 并在您的处理程序中根据 StateChange 参数的值更新您的标签
  • 您的代码在表单的constructor 方法中,但构造函数总是只运行一次(在创建表单时)。您可以查看使用 Timer 组件,例如参见youtube.com/watch?v=98c200lL-OY。我建议不要让它每秒运行一次,否则您的数据库可能会因此而变慢。
  • @PeterB 非常感谢你,我用计时器做到了,它工作得很好

标签: c# mysql winforms


【解决方案1】:

您可以监听StateChange-Event (documentation) 来更新每个可能的连接状态的标签。

在您的构造函数中,添加以下内容:

cnn.StateChange += OnStateChange;

所以它应该是这样的:

public Form1() {          
    InitializeComponent();           
    MySqlConnection cnn = new MySqlConnection(connectionString);

    cnn.StateChange += OnStateChange;

    try
    {
        cnn.Open();
    }
    catch (Exception ex)
    {
        toolStripStatusLabel1.BackColor = System.Drawing.Color.Red;
        toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
        toolStripStatusLabel1.Text = "Connection failed!";
    }
    finally
    {
        cnn.Close();
        cnn.StateChange -= OnStateChange;
    }
 }

然后你添加 eventHandler 方法:

protected virtual void OnStateChange(object connection, StateChangeEventArgs stateChange) {
    ...
}

StateChangeEventArgs 对象包含两个类型为 ConnectionState (documentation) 的属性(CurrentState 和 OriginalState),这是一个枚举。

在您的OnStateChange-方法中,您可以检查新状态是什么并相应地更新标签。

protected virtual void OnStateChange(object connection, StateChangeEventArgs stateChange) {
    if(stateChange == null || stateChange.CurrentState == null) {
        return;
    }

    switch(stateChange.CurrentState) {
        case ConnectionState.Closed:
            [UPDATE LABEL HERE]
        break;
        .
        .
        .

    }
}

【讨论】:

  • 为了做这个解决方案,我确实添加了“使用 system.data”,并且我确实遵循了你的结构。但仍然没有任何事情发生,标签也没有改变。我应该在表单中添加一些事件吗?
  • 当连接状态改变时,方法OnStateChange 会被调用吗?您是否将cnn.StateChange += OnStateChange; 添加到您的构造函数中?
  • 我得到了零错误,它仍然不能实时工作,这和我的 C# 完全一样,如果你想看到新的结果,你应该重新运行程序
  • 我很确定OnStateChange 确实not 始终准确反映数据库服务器 的状态。它所能揭示的实际上是非常有限的。参见例如这个问题的答案When is DbConnection.StateChange called?
  • @PeterB 问题不是获取服务器的当前状态,而是获取连接到服务器的当前状态。为此,OnStateChange 事件就足够了。
【解决方案2】:

我用“计时器”组件以其他方式完成了它,它工作得很好

STEP1: - 创建检查MYSQL连接状态的方法

string connectionString = "Data Source=localhost;Initial Catalog=portal;User ID=root;Password=";
    public void checkmysqlconnection()
    {
        //Real Time MYSQL Connection Stuation
        MySqlConnection cnn = new MySqlConnection(connectionString);
        try
        {
            cnn.Open();
            toolStripStatusLabel1.BackColor = System.Drawing.Color.Green;
            toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
            toolStripStatusLabel1.Text = "Connection success";
        }
        catch (Exception ex)
        {
            toolStripStatusLabel1.BackColor = System.Drawing.Color.Red;
            toolStripStatusLabel1.ForeColor = System.Drawing.Color.White;
            toolStripStatusLabel1.Text = "Connection error!";
        }
        finally
        {
            cnn.Close();

        }
    }

STEP2: - 给form添加定时器并在其上调用MYSQL Connection方法

    private Timer timer1;
    public void InitTimer()
    {
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 10000; // in miliseconds
        timer1.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        checkmysqlconnection();
    }

STEP3:- 在 Form1() 中调用计时器

 InitTimer();

它工作正常,每隔 10 秒或更短的时间检查数据库连接并更新标签状态

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多