错误:您无法添加或更改记录,因为表中需要相关记录

时间:2013-06-04 11:57:57

标签: c# winforms ms-access

当我从combox中选择Manager并输入错误的用户名和密码时,以下代码有效,错误“您无法添加或更改记录,因为表'权限'中需要相关记录”。如果我为Guest输入了错误或正确的用户名和密码,则没有错误。如果我为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;
        }
    }

Visual Studio 2010,C#,MS ACCESS 2003

提前谢谢所有人

1 个答案:

答案 0 :(得分:2)

您显然在尝试强制执行参照完整性的[LoginLogTable]和[Privileges]表之间定义了数据库关系。未知的管理器在[权限]中没有匹配的行,因此当您尝试将其(不存在的)UserName添加到[LoginLogTable]时,数据库将不允许您。

如果要记录来自未知管理器的访问尝试,则应禁用该关系的“强制参照完整性”。如果您不想记录这些尝试,那么您可以在[Privileges]表中进行查找,以便在继续之前查看UserName是否存在。如果UserName不存在,那么您可以让程序简单地终止(因为您已经告诉他们“他们已经输入了错误的凭据”)。