登录表单的麻烦 [英] Login Form trouble
问题描述
我写了下面code在登录到使用C#和LINQ我的应用程序。它连接到我在Visual Studio中创建的SQL面向服务的数据库。那我遇到的问题是,我不明白,我希望有人能帮助我这里。我创建了两个消息盒装试一试,看我的code买,我没有得到来自任何东西的输出。
如果有人可以帮助这将是伟大的!
公共BOOL用户登陆(用户字符串,字符串传递)
{
VAR数据库=新ExampleDataSet();
VAR的查询=从员工的Database.Employee
其中(Employee.EmployeeID.ToString()== Employee.ToLower()&安培;&安培; Employee.Password ==通行证)
选择员工;
如果(query.Count()!= 0)
{
返回true;
的MessageBox.show(您登录);
}
返回false;
的MessageBox.show(您没有登录);
}
私人无效cmdLogin_Click(对象发件人,EventArgs的)
{
字符串USER =(txtUser.Text);
字符串传递=(txtPass.Text);
用户登陆(用户,通行证);
}
从你写的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;
}
这篇关于登录表单的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!