当我从组合框中选择管理器并输入不正确的用户名和密码时,以下代码可以正常工作,错误“您无法添加或更改记录,因为表'权限'中需要相关记录”。如果我为访客输入不正确或正确的用户名和密码,则没有错误。如果我为 Manager 输入正确的用户名和密码,则没有错误。错误指向 cmd.ExecuteNonQuery();。请问有人可以帮我吗?
如果我删除 switch case Guest 的整个代码,从
command.CommandText = "SELECT * FROM [GuestPrivileges] WHERE STRCOMP
然后它就可以正常工作了。GuestPrivileges 表没有关系,它是独立的。
private void btnLogin_Click(object sender, EventArgs e)
{
string value = cBRoles.Text;
switch (value)
{
case "Managers":
using (var command = myCon.CreateCommand())
{
command.CommandText = "SELECT * FROM [Privileges] WHERE STRCOMP(UserName, @UserName,0) = 0 AND STRCOMP(Password, @Password,0)=0";
command.Parameters.AddWithValue("UserName", (txtUsername.Text));
command.Parameters.AddWithValue("Password", (txtPassword.Text));
myCon.Open();
var reader = command.ExecuteReader();
{
if (reader.HasRows)
{
while (reader.Read())
{
txtUsername.Text = reader["UserName"].ToString();
txtPassword.Text = reader["Password"].ToString();
MainForm frm = new MainForm();
frm.Show();
}
}
else MessageBox.Show("You have entered incorrect credentials. Please try again", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
myCon.Close();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES (@UserName, @LoggedInDate, @LoggedInTime)";
cmd.Parameters.AddWithValue("@UserName", txtUsername.Text);
cmd.Parameters.AddWithValue("@LoggedInDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@LoggedInTime", DateTime.Now.ToString("HH:mm"));
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery(); << error pointing here
cmd.CommandText = "SELECT @@IDENTITY";
int id = (int)cmd.ExecuteScalar();
this.ID = id;
myCon.Close();
break;
case "Guest":
using (var command = myCon.CreateCommand())
{
command.CommandText = "SELECT * FROM [GuestPrivileges] WHERE STRCOMP(GUserName, @GUserName,0) = 0 AND STRCOMP(GPassword, @GPassword,0)=0";
command.Parameters.AddWithValue("GUserName", (txtUsername.Text));
command.Parameters.AddWithValue("GPassword", (txtPassword.Text));
myCon.Open();
var reader = command.ExecuteReader();
{
if (reader.HasRows)
{
while (reader.Read())
{
txtUsername.Text = reader["GUserName"].ToString();
txtPassword.Text = reader["GPassword"].ToString();
ViewReport frmb = new ViewReport();
frmb.Show();
}
}
else MessageBox.Show("You have entered incorrect credentials. Please try again", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
myCon.Close();
break;
}
}
视觉工作室 2010、C#、MS 访问 2003
提前谢谢大家