当我试图从数据库中检索密码时,InvalidOperationException;“当没有数据存在时,无效尝试读取”

本文关键字:数据 存在 无效 读取 数据库 检索 密码 InvalidOperationException | 更新日期: 2023-09-27 18:19:17

这是我的代码,我需要从数据库中检索LoginPassword

public partial class frmPassword : Form
{
    SqlConnection connection = new SqlConnection(@"Data Source=WORKSTATION'SQLEXPRESS;Initial Catalog=TPSystem;Integrated Security=True");
    public frmPassword()
    {
        InitializeComponent();
    }
    private void btnUpdatePassword_Click(object sender, EventArgs e)
    {
        frmLogin login = new frmLogin();
        string UserN = login.UName;
        string Pass;
        SqlCommand cmd = new SqlCommand("SELECT Login_Password FROM AdminLogin WHERE Login_Username = '"+UserN+"'", connection);
        try
        {
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            Pass = reader["Login_Password"].ToString();
            if (tbOldPassword.Text == Pass)
                MessageBox.Show("Password matches");
            else
                MessageBox.Show("Password wrong");
            reader.Close();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, "Report", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
        }
        finally
        {
            connection.Close();
        }
    }
}

我得到这个错误在执行:

类型为"System"的未处理异常。InvalidOperationException"发生在System.Data.dll

附加信息:Invalid attempt当没有数据时读取。

当我试图从数据库中检索密码时,InvalidOperationException;“当没有数据存在时,无效尝试读取”

reader.Read()方法返回一个布尔值来指示是否实际读取了任何内容—您根本没有检查....

把你的代码改成:

if(reader.Read())
{
    Pass = reader["Login_Password"].ToString();
    if (tbOldPassword.Text == Pass)
            MessageBox.Show("Password matches");
    else
            MessageBox.Show("Password wrong");
}

如果什么都没读,我猜你的用户不存在-我不知道在这种情况下你想做什么....由你决定

但是告诉我你不是明文中存储密码在您的数据库!!!!

似乎数据库中没有您正在检查的用户名条目。请检查阅读器是否返回结果:

public partial class frmPassword : Form
{
    SqlConnection connection = new SqlConnection(@"Data Source=WORKSTATION'SQLEXPRESS;Initial Catalog=TPSystem;Integrated Security=True");
    public frmPassword()
    {
        InitializeComponent();
    }
    private void btnUpdatePassword_Click(object sender, EventArgs e)
    {
        frmLogin login = new frmLogin();
        string UserN = login.UName;
        string Pass;
        SqlCommand cmd = new SqlCommand("SELECT Login_Password FROM AdminLogin WHERE Login_Username = '"+UserN+"'", connection);
        try
        {
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
            {
            Pass = reader["Login_Password"].ToString();
            if (tbOldPassword.Text == Pass)
                MessageBox.Show("Password matches");
            else
                MessageBox.Show("Password wrong");
            }
            reader.Close();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, "Report", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
        }
        finally
        {
            connection.Close();
        }
    }
}
相关文章: