2016-12-03 86 views
0

因此,我正在为一个类的项目工作。第一部分是登录表单,它要求用户输入用户名和密码。当登录按钮被击中时,程序将比较文本框文本和数据表中的内容。唯一的问题是,我这样做很难。我试着用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是数据库本身。

+0

“我试着用LINQ语句做这件事,但是这使得这些值与我期待的不同。”好的,请告诉我们你的期望和实际得到的结果。 – ChrisF

+0

另外,你为什么要在已经是字符串的东西上调用'Convert.ToString'? – ChrisF

+0

我期待用户和用户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 } –

回答

1

我不是LINQ to SQL的用户,但我相信以下内容对您有用。

基本上,我做了如下修改:1。 结合的用户名和密码,检查到一个单一的WHERE子句 2.如果你得到一个匹配的记录后面(即Enumerable.Count检查)这意味着用户名和密码相匹配一个记录,因此是正确的。

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(); 
    }*/ 

    var user = 
     from use in dbcontext.UserTables 
     where use.UserName == textBox1.Text && use.Password == textBox2.Text 
     select use; 

    if (Enumerable.Count(user) > 0) 
    { 
     ShowSelectForm ssf = new ShowSelectForm(this, sort); 
     Hide(); 
     ssf.Show(); 
    } 

    else 
    { 
     MessageBox.Show("Incorrect username and/or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     textBox1.Focus(); 
    } 
} 
+0

确实解决了这个问题。谢谢您的帮助。 –

+0

没有问题。你介意接受我写的答案吗? –

0

我希望您的数据源已填写且密码未在数据库中加密。

var user = dbcontext.UserTables.FirstOrDefault(u => u.UserName == textBox1.Text && u.UserPassword == textBox2.Text); 

if(user != null) 
    // Check 
else 
    // Failed 

如果textBox1是用户名和textBox2是密码,这应该工作。

相关问题