我是否正确连接到数据库?
连接时出现错误adapter.Fill(table);
using MySql.Data.MySqlClient;
...
private void buttonOk_Click(object sender, EventArgs e)
{
timer2.Start();
}
public static string Datasource = "localhost";
public static string Port = "3306";
public static string InitialCatalog = "user";
public static string Username = "user";
public static string Password = "pass";
//MySql
MySqlConnection connection = new MySqlConnection("datasource=" + Datasource + ";port=" + Port + ";Initial Catalog='" + InitialCatalog + "';username=" + Username + ";password=" + Password);
MySqlDataAdapter adapter;
DataTable table = new DataTable();
public void MySqlConnect()
{
adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + textBoxUsername.Text + "' AND `password` = '" + textBoxPassword.Text + "'", connection);
adapter.Fill(table); //PROBLEM
if (table.Rows.Count <= 0)
{
panel1.Height = 0;
labelMessage.ForeColor = Color.Red;
labelMessage.Text = "Username Or Password Are Invalid";
timer1.Start();
}
else
{
panel1.Height = 0;
labelMessage.ForeColor = Color.Green;
labelMessage.Text = "Login Successfully";
timer1.Start();
}
table.Clear();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (panel1.Height != 100)
{
panel1.Height = panel1.Height + 5;
if (panel1.Height == 100)
{
timer1.Stop();
}
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (panel1.Height != 0)
{
panel1.Height = panel1.Height - 5;
if (panel1.Height == 0)
{
timer2.Stop();
}
}
}
private void checkBoxShowPass_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxShowPass.Checked)
{
textBoxPassword.UseSystemPasswordChar = true;
}
else
{
textBoxPassword.UseSystemPasswordChar = false;
}
}
...
错误文本
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Сообщение = Authentication to host 'localhost' for user 'user' using method 'mysql_native_password' failed with message: Access denied for user 'user'@'localhost' (using password: YES)
Источник = MySql.Data
Внутреннее исключение 1:
MySqlException: Access denied for user 'user'@'localhost' (using password: YES)
根据错误判断,此帐户无权访问此数据库。
尝试使用 IDE 连接到数据库。我相信你会得到同样的错误=>你的代码与它无关,你需要挖掘权利。
我有一个,100% 工作