更新时间:2023-12-04 18:11:46
从你写的code,似乎问题是,你比较字符串重的presentation 员工
及其雇员
属性 Employee.EmployeeID.ToString()== Employee.ToLower()
。这条线将随时返回假
除非覆盖的ToString()
员工的方法
类返回的财产雇员
(我presume你没有)。
试试这个,而不是(假设参数用户
包含了用户的姓名):
使用(var数据=新ExampleDataSet())
{
VAR的loggedIn = dataSet.Employee.Any(E => e.UserName ==用户放大器;&安培; e.Password ==通行证);
VAR消息=的loggedIn? 您登录:您还没有登录;
的MessageBox.show(消息);
返回的loggedIn;
}
I wrote the following code to log in to my application using C# and LINQ. It connected to a SQL service oriented database that I have created in Visual Studio. The problem that I am having is one that I do not understand and am hoping that someone can help me about here. I have created two message boxed to try to see the output of my code buy I am not getting anything from it.
If anyone could help that would be great!
public bool UserLogin(string User, string Pass)
{
var Database = new ExampleDataSet();
var query = from Employee in Database.Employee
where (Employee.EmployeeID.ToString() == Employee.ToLower() && Employee.Password == Pass)
select Employee;
if (query.Count() != 0)
{
return true;
MessageBox.Show("You are logged in");
}
return false;
MessageBox.Show("You are not logged in");
}
private void cmdLogin_Click(object sender, EventArgs e)
{
string User = (txtUser.Text);
string Pass = (txtPass.Text);
UserLogin(User, Pass);
}
From the code you wrote, it seems that the problem is that you compare the string representation of an Employee
with its EmployeeId
property Employee.EmployeeID.ToString() == Employee.ToLower()
. This line will always return false
unless you override ToString()
method of Employee
class to return the property EmployeeId
(which I presume you didn't).
Try this instead (assuming that parameter User
contains the name of the user):
using(var dataSet = new ExampleDataSet())
{
var loggedIn = dataSet.Employee.Any(e=>e.UserName == User && e.Password == Pass);
var message = loggedIn ? "You are logged in" : "You are not logged in";
MessageBox.Show(message);
return loggedIn;
}