为什么我会“连接必须有效并且打开错误”?这之前很好。嗯 [英] Why Am I Getting "Connection Must Be Valid And Open Error"? This Was Ok Before. Hmm

查看:90
本文介绍了为什么我会“连接必须有效并且打开错误”?这之前很好。嗯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  button1_Click(对象发​​件人,EventArgs e)
{
使用(MySqlConnection con = new MySqlConnection( Server = localhost; UID = root; Database = db_cignal) )
{
con.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = con;
command.CommandText = SELECT * FROM Admin;
command.Prepare();
MySqlDataReader read = command.ExecuteReader();

while (read.Read())
{

if (txtName.Text.Equals(读取[ 用户名]。 ToString())&& txtPass.Text.Equals(读取[ 密码]。ToString ()))
{
MessageBox.Show( ***登录成功*** );
this .Hide();
管理员表格2 = 管理员();
form2.Closed + =(s,args)= > .Close() ;
form2.Show();
}
else
{
MessageBox.Show( 请输入正确的用户名或密码..);
txtName.Text = ;

txtPass.Text = ;
}

}
read.Close();
con.Close();
}
}

解决方案

最可能的原因是连接字符串:代码中的固定字符串经常给出问题。你之前没有说过你在做什么,或者之后发生了什么变化,但是这种事情往往是因为你指定localhost作为服务器,然后移动到另一台计算机 - 数据库服务器未安装。



我建议您将连接字符串移动到某个描述的配置文件中,这样您就可以在不重新编译的情况下对其进行更改,并查看环境您正在尝试运行该应用程序。具体来说,查看安装的数据库服务器,看看是否需要不同的连接字符串 - 不同的服务器名称,实际登录等等。



我们可以为你做到这一点 - 我们无法访问你的系统! :笑:



BTW:绝不以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]

private void button1_Click(object sender, EventArgs e)
        {
            using (MySqlConnection con = new MySqlConnection("Server=localhost;UID=root;Database=db_cignal"))
            {
                con.Open();
                MySqlCommand command = new MySqlCommand();
                command.Connection = con;
                command.CommandText = "SELECT * FROM Admin";
                command.Prepare();
                MySqlDataReader read = command.ExecuteReader();

                while (read.Read())
                {

                    if (txtName.Text.Equals(read["Username"].ToString()) && txtPass.Text.Equals(read["Password"].ToString()))
                    {
                        MessageBox.Show("***Login Successfully***");
                        this.Hide();
                        Administrator form2 = new Administrator();
                        form2.Closed += (s, args) => this.Close();
                        form2.Show();
                    }
                    else
                    {
                        MessageBox.Show("Please input the correct username or password..");
                        txtName.Text = "";

                        txtPass.Text = "";
                    }

                }
                read.Close();
                con.Close();
                }
            }

解决方案

The most likely reason is the connection string: fixed strings in your code often give problems. You don't say what you were doing before when it worked, or what has changed since then, but this kind of thing is often because you specify "localhost" as the server, and then move to a different computer - where the DB server is not installed.

I'd suggest that you move your connection string to a config file of some description so you can alter it without recompilation, and look at the environment in which you are trying to run the application. Specifically, look at the DB servers installed, and see if you need a different connection string - a different server name, an actual login, and so forth.

We can't do that for you - we can't access your systems! :laugh:

BTW: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]


这篇关于为什么我会“连接必须有效并且打开错误”?这之前很好。嗯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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