2016-12-03 1 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

사용자 및 user2가 텍스트 상자의 사용자 이름 및 암호 값과 같을 것으로 예상했습니다. 대신 이런 식으로 뭔가로 나왔다 :... + \t가 \t 사용자 \t { [Extent1]에서 [사용자 ID] AS [사용자 ID], [Extent1] [이름] AS [사용자 이름], [Extent1] [UserPassword를] [UserPassword] FROM [dbo]. [UserTable] AS [Extent1] WHERE [Extent1]. [UserName] = @ p__linq__0} \t 시스템. 클라이언트. DbQuery } –

답변

1

저는 LINQ to SQL의 사용자가 아니지만 다음 사항이 유용 할 것이라고 생각합니다.

기본적으로, 나는 다음과 같이 변경했다 : 1. 당신이 다시 일치하는 레코드를 받으면 절 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

귀하의 데이터 소스가 채워지고 귀하의 비밀번호가 데이터베이스에서 암호화되지 않았기를 바랍니다. textBox1 사용자 이름이고 textBox2 암호 인 경우

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

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

이 작동합니다.

관련 문제