因此,我正在为一个类的项目工作。第一部分是登录表单,它要求用户输入用户名和密码。当登录按钮被击中时,程序将比较文本框文本和数据表中的内容。唯一的问题是,我这样做很难。我试着用LINQ语句来做这件事,但是这使得这些值与我去调试时期望的值不同。我在这里做错了什么?这是表单的代码。将文本框值与数据库进行比较
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Entity;
using System.Data.Entity.Validation;
namespace mcshonsey_Final
{
public partial class LoginForm : Form
{
SortingClass sort = new SortingClass();
mcshonsey_FinalProject.UserShowDBEntities dbcontext = null;
public LoginForm()
{
InitializeComponent();
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
if (dbcontext != null)
dbcontext.Dispose();
dbcontext = new mcshonsey_FinalProject.UserShowDBEntities();
dbcontext.UserTables
.OrderBy(entry => entry.UserID)
.Load();
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("You must enter a password or username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
/*else
{
ShowSelectForm ssf = new ShowSelectForm(this, sort);
Hide();
ssf.Show();
}*/
string num1 = Convert.ToString(textBox1.Text);
string num2 = Convert.ToString(textBox2.Text);
var user =
from use in dbcontext.UserTables
where use.UserName == num1
select use;
var user2 =
from pas in dbcontext.UserTables
where pas.UserPassword == num2
select pas;
if (textBox1.Text.Equals(user) && textBox2.Text.Equals(user2))
{
ShowSelectForm ssf = new ShowSelectForm(this, sort);
Hide();
ssf.Show();
}
else
{
MessageBox.Show("Incorrect username and/or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Exit();
}
}
private void LoginForm_Load(object sender, EventArgs e)
{
}
}
}
的SortingClass是通过数据表进行排序的一类,但是这是在稍后的时间。 UserShowDBEntities是数据库本身。
“我试着用LINQ语句做这件事,但是这使得这些值与我期待的不同。”好的,请告诉我们你的期望和实际得到的结果。 – ChrisF
另外,你为什么要在已经是字符串的东西上调用'Convert.ToString'? – ChrisF
我期待用户和用户2与文本框的用户名和密码值相同。相反,它出来的东西是这样的:。+ \t \t用户\t {选择 [Extent1] [用户名] AS [用户名], [Extent1] [用户名] AS [用户名], [Extent1] [的userPassword] AS [UserPassword] FROM [dbo]。[UserTable] AS [Extent1] WHERE [Extent1]。[UserName] = @ p__linq__0} \t System.Linq.IQueryable {System.Data.Entity.Infrastructure。 DbQuery } –