2016-12-03 72 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是密碼,這應該工作。

相關問題