gpt4 book ai didi

c# - 连接到 MySQL 数据库 C#

转载 作者:行者123 更新时间:2023-11-29 23:20:48 24 4
gpt4 key购买 nike

我需要连接到我的网络主机上的 MySQL 服务器的帮助。这是我的登录按钮的代码,但它无法让我登录。

private void btnLogin_Click(object sender, EventArgs e)
{
try
{
string strConnect = "Server=localhost;Database=cpr_users;Uid=MyUsername;password=*******;";
MySqlConnection myConn = new MySqlConnection(strConnect);
MySqlCommand selectCmd = new MySqlCommand("select * from cpr_users.cpr_user_info where username='" + txtUsername.Text + "' and password='" + txtPassword.Text + "' ;", myConn);
MySqlDataReader myReader;
myConn.Open();

int count = 0;
myReader = selectCmd.ExecuteReader();
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
this.Close();
MedicalForm();
}
else if (count > 1)
{
MessageBox.Show("Theres 2 Users with that username Please contact Your_Name_Here ...Access Denied");
}
else
MessageBox.Show("Username or Password is Not correct .. Please try Again!");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

我不确定我是否使用了错误的连接信息

当我使用虚拟主机的专用 IP 并登录时,出现以下错误

Authentication to host '**IP ADDRESS**' for user 'MyUsername' using method 'mysql_native_password' failed with message: Access denied for user 'MyUsername'@'myipaddress' (using password: YES)

最佳答案

尝试将 Server=localhost 更改为 Server=127.0.0.1(如果有效)验证您的 hosts 文件并设置正确的重定向.. .否则用 Mysql 提示验证用户和密码是否存在(如果存在)验证它是否正确写入连接字符串

关于c# - 连接到 MySQL 数据库 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com