Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#winforms登录无法连接到mysql(错误40)/登录不起作用_C#_Mysql_Winforms - Fatal编程技术网

C#winforms登录无法连接到mysql(错误40)/登录不起作用

C#winforms登录无法连接到mysql(错误40)/登录不起作用,c#,mysql,winforms,C#,Mysql,Winforms,您好,我已经用互联网示例中的mysql db创建了简单的登录表单。我将xampp用于本地主机mysql托管。但每次我尝试使用登录表单时,我都会收到这条消息。 它似乎无法连接到数据库,但我无法找到原因。 我认为我的连接字符串很好,但没有什么想法,它可能的代码也很糟糕。 我尝试过:使用不同的sql托管, 在phpmyadmin中设置权限, 正在创建连接到数据库的新用户, 使用不同的连接字符串 private void btnLogin_Click(object sender, EventArgs e

您好,我已经用互联网示例中的mysql db创建了简单的登录表单。我将xampp用于本地主机mysql托管。但每次我尝试使用登录表单时,我都会收到这条消息。 它似乎无法连接到数据库,但我无法找到原因。 我认为我的连接字符串很好,但没有什么想法,它可能的代码也很糟糕。 我尝试过:使用不同的sql托管, 在phpmyadmin中设置权限, 正在创建连接到数据库的新用户, 使用不同的连接字符串

private void btnLogin_Click(object sender, EventArgs e)
    {
        String result = "";
        try
        {
            con = new SqlConnection(@"Data source=localhost;Initial Catalog=users;User ID=root;Password=test.test;Integrated Security=True");
            cmd = new SqlCommand("select * from tblLogininfo where UserId=@uid and Password=@password", con);
            con.Open();
            cmd.Parameters.AddWithValue("@uid", txtUserId.Text.ToString());
            cmd.Parameters.AddWithValue("@password", txtPassword.Text.ToString());
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                if (reader["Password"].ToString().Equals(txtPassword.Text.ToString(), StringComparison.InvariantCulture))
                {
                    UserInfo.empid = reader["EmpId"].ToString();
                    UserInfo.userid = reader["UserId"].ToString();
                    UserInfo.role = reader["UserRole"].ToString();
                    result = "1";
                }
                else
                    result = "Invalid credentials";
            }
            else
                result = "Connection failed";

            reader.Close();
            cmd.Dispose();
            con.Close();

        }
        catch (Exception ex)
        {
            result = ex.Message.ToString();
        }

        if (result == "1")
        {
            Program.openDashboard = true;
            this.Close();
        }
        else
            MessageBox.Show(result);
    }

根据varius的评论,您需要包含对MySQL的引用。因此,右键单击“参考”,在解决方案资源管理器选项卡->管理NuGet软件包->浏览选项卡->搜索“mysql”->安装mysql.Data

下载后,使用MySql.Data.MySqlClient添加


此时,替换所有出现的SQL使用MySQL

SqlConnection
是用于SQL Server的,不是MySQL,您正在MySQL中查找数据库,并且您正在使用SQL API,请从nuget下载MySQL dll并使用它