gpt4 book ai didi

c# - 否则在使用 c# 的阅读器中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:58 25 4
gpt4 key购买 nike

当我输入用户名和密码时,它成功登录并打开了所需的字段,但是当我输入错误的名称或密码(未保存在数据库中)时,它实际上什么也没显示,它应该遇到“其他”,这是代码

private void signin_button_Click(object sender, EventArgs e)
{
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:/Student_Managment_System/SMS1.mdb";

OleDbConnection myConnection = new OleDbConnection(connectionString);
myConnection.Open();

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = myConnection;

string usrname = name_textBox.Text;
string passwd = pass_textBox.Text;

OleDbCommand cmd1 = new OleDbCommand("select * from Manager where Name='" + usrname + "' and Passwd='" + passwd + "'");

OleDbDataReader Reader = cmd1.ExecuteReader();

while (Reader.Read())
{
if (Reader[5].ToString() == "manager")
{
this.Hide();
Student_Info stuInf = new Student_Info();
stuInf.Show();
break;
}
else if (Reader[5].ToString() == "employee")
{
MessageBox.Show("log in as a employee ");
}
else
{
MessageBox.Show("Inviled User name or password");
}
}

myConnection.Close();
}

最佳答案

您的 else 语句未执行,因为在输入无效的用户名或密码时没有可读取的值。您需要检查您的阅读器是否有任何行。参见 here

代码

//Are there any rows
if(Reader.HasRows)
{
//If so read them
while (Reader.Read())
{
if (Reader[5].ToString() == "manager")
{
this.Hide();
Student_Info stuInf = new Student_Info();
stuInf.Show();
break;
}
else if (Reader[5].ToString() == "employee")
{
MessageBox.Show("log in as a employee ");
}
}
}
else
{
MessageBox.Show("Inviled User name or password");
}

关于c# - 否则在使用 c# 的阅读器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com