我正在尝试使用Windows窗体c#创建一个登录系统,但它给了我一个错误。我正在使用mysql数据库和youtube的教程。我不知道这是什么错误。该项目包含三种形式:登录一,寄存器一和主程序。
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace LoginSys
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitB_Click(object sender, EventArgs e)
{
this.Close();
}
private void registerB_Click(object sender, EventArgs e)
{
Register reg = new Register();
reg.Show();
this.Hide();
}
private void loginB_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Teo\Documents\Databases\Login\Data.mdf;Integrated Security=True;Connect Timeout=30");
string query = "select * from Table where username = " + username.Text.Trim() + " and password = " + password.Text.Trim() + ");";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
if(dt.Rows.Count >= 1)
{
Register reg = new Register();
reg.Show();
this.Hide();
}
else
{
MessageBox.Show("Incorrect Password","Error");
}
}
}
} `
答案 0 :(得分:1)
我相信Table
是MySQL / SQL Server中的保留关键字,也许您在复制代码时忘了重写它。确保数据所在的表格
select * from Table where username = " + username.Text.Trim() + " and password = " + password.Text.Trim() + ");";
^使用适当的表更改表。
答案 1 :(得分:0)
Table
是一个真正保留的关键字。
请试试这个:
select * from [Table] ...