简体   繁体   English

将文本框值与数据库进行比较

[英]Comparing Textbox values to a database

So, I'm working on a project for a class of mine. 因此,我正在为某类我的项目工作。 The first part is a login form, which requires a user to enter a username and a password. 第一部分是登录表单,要求用户输入用户名和密码。 When the login button is hit, the program is to compare the textbox text to what is in a datatable. 当单击登录按钮时,程序将比较文本框文本和数据表中的文本。 Only problem is, I'm having a tough time doing this. 唯一的问题是,我很难做到这一点。 I tried doing it with LINQ statements, but that made the values different from what I was expecting when I went to debug it. 我尝试使用LINQ语句来执行此操作,但是这使值与调试调试时的预期值有所不同。 Am I doing something wrong here? 我在这里做错什么了吗? Heres the code for the form. 这是表格的代码。

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)
    {

    }
}
}

The SortingClass is a class to sort through the datatable, but that's for a later time. SortingClass是用于对数据表进行排序的类,但这是以后的事情。 The UserShowDBEntities is the database itself. UserShowDBEntities是数据库本身。

I'm not a user of LINQ to SQL but I believe the following would work for you. 我不是LINQ to SQL的用户,但我相信以下内容对您有用。

Basically, I made the following changes: 1. Combined the username and password check into a single WHERE clause 2. If you get a matching record back (ie Enumerable.Count check) it means the username and password matched a record and thus were correct. 基本上,我进行了以下更改: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();
    }
}

I hope your data source is filled and your password is not encrypted in the database. 希望您的数据源已满,并且数据库中的密码未加密。

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

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

If textBox1 is the username and textBox2 is the password, this should work. 如果textBox1是用户名, textBox2是密码,则应该可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM