Encoding.ASCII.GetString(o.Username) == textBox1.Text
这是我以前用来解决这个问题的代码。我不知道在我的代码中把它放在哪里。请帮忙!!
这是错误的部分:
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Please Enter your username.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
return;
}
try
{
using (DataEntities test = new DataEntities())
{
var query = from o in test.Users
where o.Username == textBox1.Text && o.Password == textBox2.Text
select o;
if(query.SingleOrDefault() != null)
{
MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
//Add your code process login here
}
else
{
MessageBox.Show("Your username or password is incorrect.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 0 :(得分:-2)
当您尝试编译时,您收到此错误,对吗?
我认为您的问题实际上是您的查询没有引号。
此:
var query = from o in test.Users
where o.Username == textBox1.Text && o.Password == textBox2.text
select o;
应该是:
var query = "from o in test.Users" +
"where o.Username == textBox1.Text && o.Password == textBox2.text" +
"select o";
query是一个字符串,所以你需要用引号包围=的所有内容。这告诉编译器它是一个字符串文字,而不是更多的代码。