【问题标题】:Close database connection after launch second form启动第二种形式后关闭数据库连接
【发布时间】:2015-01-09 10:40:06
【问题描述】:

在我的应用程序中,我有两个表单。一个用于登录,另一个用于实际应用程序。但是在登录表单成功登录数据库连接后不会终止。关闭此连接的唯一方法是关闭这两种形式。但我想在成功登录后立即关闭登录表单建立的数据库连接。这是我的代码

private void button2_Click(object sender, EventArgs e)
        {


            try
            {


            string mysqlconnection = string.Format("datasource='{0}';username=uwcentrallogin;port=3306;password=**************;Connect Timeout=20000;Command Timeout=28800", serverip.Text);
            MySqlConnection myconn = new MySqlConnection(mysqlconnection);
            MySqlCommand Selectcommand = new MySqlCommand("select * from wartif.userdata where username='" + this.adminusername.Text.Trim() + "'and adminpassword= '" + this.passwordtext.Text.Trim() + "' ; ", myconn);

            MySqlDataReader myreader;
            myconn.Open();
            myreader = Selectcommand.ExecuteReader();
            int count = 0;

            while (myreader.Read())
            {
                count = count + 1;

            }

            if (count == 1)
            {



                this.Hide();
                adminview f2 = new adminview(serverip.Text, adminusername.Text, portnumberbox.Text, defdatabase.Text);
                f2.ShowDialog();
                this.Close();
                myconn.Close();

            }

            else if (count > 1)
            {


                label4.Text = "duplicatie users exsist ";
            }

            else

            label4.Text = "Not a privileged user";
            label4.ForeColor = Color.Orange;
            errpan.BackColor = Color.Orange;


            myconn.Close();

        }

        catch 
        {


            label4.Text = "mysql database connection is not avialable";
        }



    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Close();
    }


    private void button4_Click(object sender, EventArgs e)
    {


        appconfigsave();

        label4.Text = "Your changes has been saved";
        label4.ForeColor = Color.Orange;
        errpan.BackColor = Color.Orange;

    }

【问题讨论】:

  • 根本不是您的问题的答案,而是:真正命名您的标签和按钮,如果您的应用程序变得更大,您将失去对哪个数字是什么按钮/标签的所有控制。它也会让你的代码更清晰。
  • 在 SQL 命令中使用参数来防止 SQL 注入。
  • @Sybren 感谢我将其更改为参数化 sql 的建议

标签: c# mysql winforms connection-string


【解决方案1】:

添加:

 myconn.Dispose();

在你的线下:

myconn.Close();

我还建议您查看Using Statments

更多信息here

示例

        if (count == 1)
        {
            this.Hide();
            adminview f2 = new adminview(serverip.Text, adminusername.Text, portnumberbox.Text, defdatabase.Text);
            myconn.Close();
            myconn.Dispose();
            f2.ShowDialog();
            this.Close();
        }

【讨论】:

  • 我会说您需要将myconn.Close(); 行移到f2.ShowDialog(); 上方并将我的Dispose() 行添加到您的Close() 下方我之所以这么说是因为如果您的对话f2尝试在初始化时使用 SQLConnection 将无法使用,因为您尚未关闭/处理您的连接。在编辑中查看我的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-04
  • 2011-07-20
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
相关资源
最近更新 更多