그래서 저는 광산 프로젝트를 진행하고 있습니다. 첫 번째 부분은 사용자가 사용자 이름과 암호를 입력해야하는 로그인 양식입니다. 로그인 버튼을 누르면 프로그램은 텍스트 상자 텍스트를 데이터 테이블에있는 텍스트와 비교합니다. 문제는, 나는 이것을하기가 힘들다. 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는 데이터베이스 자체입니다.
"LINQ 문으로 처리하려고 시도했지만 예상 한 것과 다른 값을 만들었습니다." 좋아요, 그래서 당신이 예상했던 것과 실제로 얻은 것을 말해주십시오. – ChrisF
또한 왜 당신은'Convert.ToString'을 이미 문자열 인 것을 호출하고 있습니까? – ChrisF
사용자 및 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} –