这是我的代码..我试图让它来验证数据库。
传递/用户=管理员
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void main_B_Signup_Click(object sender, RoutedEventArgs e)
{
RegWindow rWindow = new RegWindow();
rWindow.Show();
this.Close();
}
private void main_B_login_Click(object sender, RoutedEventArgs e)
{
//connect to the database
SqlConnection loginConn = new SqlConnection("server=localhost;"+ "Trusted_Connection=yes;" + "database=Production; " + "connection timeout=30");
SqlCommand cmd = new SqlCommand("Select *from User where Username=' " + this.Main_T_Username.Text + " ' and Password=' " + this.Main_T_Password.Text + " ' ;", loginConn);
//SqlCommand cmd = new SqlCommand("Select *from User where Username='@Username' and Password='@Password';", loginConn);
//cmd.Parameters.Add(new SqlParameter("Username", this.Main_T_Username.Text));
//cmd.Parameters.Add(new SqlParameter("Password", this.Main_T_Password.Text));
loginConn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
string username = null;
if (rdr.HasRows)
{
while (rdr.Read())
{
username = rdr["Username"].ToString();
}
loginConn.Close();
MessageBox.Show("Well done!");
}
else
{
MessageBox.Show("WrongPass!");
loginConn.Close();
}
}
}
}
但我得到的错误是
关键字&#39;用户&#39;
附近的语法不正确
但该表名为User
,并且列Username
和Password
答案 0 :(得分:7)
“用户”是SQL Server中的reserved word。要将其用作架构对象的标识符,请使用方括号括起来:
SELECT * FROM [User]
通常,最好使用模式对象标识符来执行此操作。它使它们在查询中更明确。
此外,你是:
答案 1 :(得分:0)
将单词User放在方括号中,如[User]
,因为它是SQL中已定义的关键字。
答案 2 :(得分:0)
某些字词受到保护
试
Select * from [User] where Username.....