【发布时间】:2015-06-22 16:11:55
【问题描述】:
我有一个名为 Logins 的数据库表,它有 3 列:Username、Password 和 Category。类别有两种类型:Admin 和 User。
我有一个带有两个文本框的表单,用户将在其中写下他的用户名和密码。使用用户名文本框输入,我想检查给定的用户名是Admin 还是User,并在此基础上打开不同的表单。
我收到以下错误:
意外错误:
"'"附近的语法不正确
SqlConnection con = new SqlConnection("Data Source=JAYI-PC\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
try
{
con.Open();
SqlCommand cmd = new SqlCommand(@"SELECT Count(*) FROM Logins
WHERE Username=@uname and
Password=@pass,Category=@ctgy", con);
cmd.Parameters.AddWithValue("@uname", textBox_usern.Text);
cmd.Parameters.AddWithValue("@pass", textBox_pwd.Text);
cmd.Parameters.AddWithValue("@ctgy", c); //Can't understand how to check it
int result = (int)cmd.ExecuteScalar();
if (result > 0)
{
if (c== "Admin")//this one will chek whether user is admin or user
{
MessageBox.Show("Welcome Admin");
Admin f1 = new Admin();
f1.Show();
}
else
{
MessageBox.Show("Welcome " + textBox_usern.Text);
FormCheck f3 = new FormCheck();
f3.Show();
}
}
else
{
MessageBox.Show("Incorrect login");
}
textBox_usern.Clear();
textBox_pwd.Clear();
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error:" + ex.Message);
}
【问题讨论】:
-
您是否以纯文本形式存储密码?请不要那样做。
-
MAV 是初学者。那我该怎么办?请帮忙
-
@JayitaRoy 你从哪里分配 C 的值?
-
删除
Category=@ctgy和cmd.Parameters.AddWithValue("@ctgy", c);。您不是根据category选择的,您需要通过username和pass获取它。选择仅使用username和pass后,检查该行是category == "Admin"还是"User"。 -
Sachu 问题是我不明白如何处理 Category=@ctgy