C# 从数据库中选择用户和密码时出现用户登录表单问题

C# 从数据库中选择用户和密码时出现用户登录表单问题,c#,sql,visual-studio,visual-studio-2012,user-input,C#,Sql,Visual Studio,Visual Studio 2012,User Input,我在下面的代码中有用户登录表单(用户名和密码),我希望它检查用户和密码是否存在于数据库中,但当输入用户和密码时,它不允许我访问表单2。我总是收到一条错误登录消息,我认为与数据库没有连接,或者它没有读取if条件 我的表名为account,包含两列(user、pass) 数据库名称数据库1 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syst

我在下面的代码中有用户登录表单(用户名和密码),我希望它检查用户和密码是否存在于数据库中,但当输入用户和密码时,它不允许我访问表单2。我总是收到一条错误登录消息,我认为与数据库没有连接,或者它没有读取if条件

我的表名为account,包含两列(user、pass) 数据库名称数据库1

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;

namespace testdb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;";


    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            SqlConnection cn = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("Select * from account where user=@username and Pass=@password", cn);
            cmd.Parameters.AddWithValue("@username", textBox2.Text);
            cmd.Parameters.AddWithValue("@password", textBox1.Text);

            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cn.Close();
            int count = ds.Tables[0].Rows.Count;
            if (count == 1)
            {

                Form2 form2 = new Form2();
                this.Hide();
                form2.Show();
            }
            else
            {
                MessageBox.Show("Invalid Login please check username and password");
            }
            cn.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        }

}
}

这条线

 SqlConnection cn = new SqlConnection(global::testdb.Properties.Settings.Default.Database1ConnectionString);
我把它改成了

string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;";
注意:db它存在于visual studio中意味着我正在使用visual studio附带的db,并且我已经用用户名和密码定义了表帐户


这是我的问题,即使用户和密码正确,我也会收到错误消息

这里是我的数据库到VS


其他快照可以在我的回复中找到

在登录时,系统基本上要求用户告诉它两件事:

  • 这就是我的身份(用户名、电子邮件地址等),以及
  • 这是我知道的(密码)
所以当你评估用户的输入时,你应该用同样的方法;也就是说,首先检查用户的身份(我是谁),然后比较密码(我知道的)

考虑以下示例:

protected void btnLogin_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection cn = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand("Select * from account where user=@username", cn);
        cmd.Parameters.AddWithValue("@username", textBox2.Text);

        cn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();

        if (ds.Tables[0].Rows.Count == 1)
        {
            // A user with this username exists, check the password.
            if (ds.Tables[0].Rows[0][1] == textBox1.Text)
            {
                // The login succeeded, show Form2.
                Form2.Show();
            }
            else
            {
                MessageBox.Show("Invalid Login please check username and password");
            }
        }
        else
        {
            // No user with the given username exists.
            MessageBox.Show("Invalid Login please check username and password");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
如果没有具有指定用户名的用户,则会出现“无效登录”错误

如果用户登录时使用的密码与数据库中的密码不匹配,则会出现“无效登录”错误

如果系统无法连接到数据库,您将从
catch
块收到一条错误消息


如果一切顺利,用户登录时应显示Form2。

您是否尝试过对此进行调试?找到调试器将停止的位置,并记下调试器给您的消息/警告/错误。您总是收到“无效登录,请检查用户名和密码”错误消息?如果是,则您的数据库连接工作正常,但如果您收到一些异常消息,则DB call中存在问题。如果不知道您的数据是什么样子或您遇到了什么错误,我们将无法帮助您。请更新您的问题以包含更多详细信息。这里是已定义的用户,有时我在按刷新按钮[1]时出错:错误[2]:谢谢,我尝试了您的代码没有错误我还将此行更改为ds.Tables[0]。行[0][1]==textBox1.Text)到ds.Tables[0]。行[0][1].ToString()==textBox1.Text)将其转换为字符串,因为我们有对象和字符串,但没有任何更改:(参见上面的快照,我在那里更新了我的问题