没有得到正确的数据

时间:2018-04-12 02:36:12

标签: c# ms-access

在我的登录表单中我设置了用户名和密码,之后我在Employee_Details上设置了一个查询,如果它成功登录就会在另一个表上写入历史记录参考,所以我设置了用户名和登录日期但是在我的Employee_History表中写入时总是得到用户名和密码。请帮助我。

private void signin_Click(object sender, EventArgs e)
        {
            if (IsValidated())
            {
                try
                {
                    DataTable dt = new DataTable();
                    String Account_Type;          
                    OleDbConnection con = new OleDbConnection();
                    con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\dbms\jollibee.accdb";
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "select * from Employee_Details where username = @username and password = @password and account_Type";

                    cmd.Parameters.AddWithValue("@username", username.Text);
                    cmd.Parameters.AddWithValue("@password", password.Text);

                    OleDbDataReader read = cmd.ExecuteReader();
                    dt.Load(read);

                    if(dt.Rows.Count > 0)
                    {
                        Account_Type = dt.Rows[0][3].ToString().Trim();
                        MessageBox.Show("Login Successfull.", "Success", MessageBoxButtons.OK, MessageBoxIcon.None);

                        if (Account_Type.Equals("Administrator"))
                        {
                            admin a = new admin();
                            a.Show();
                            this.Hide();
                        }
                        else if (Account_Type.Equals("Manager"))
                             {
                            supervisor s = new supervisor();
                            s.Show();
                            this.Hide();
                        }
                        else if (Account_Type.Equals("Cashier"))
                             {
                            cashier c = new cashier();
                            c.Show();
                            this.Hide();
                        }

                    } else
                    {
                        MessageBox.Show("Please check your username and password if correct. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    cmd.CommandText = "insert into Employee_History (username, DateLogin) values (?,?);";
                    cmd.Parameters.AddWithValue("@username", username.Text);
                    cmd.Parameters.AddWithValue("@DateLogin", DateTime.Now.ToString("MM/dd/yyyy HH:mm"));
                    cmd.ExecuteNonQuery();
                    con.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex.Message, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

您正在使用位置参数而不清除现有参数。简单地设置新的命令文本不会清除"命令,你必须自己清除它:

cmd.Parameters.Clear();   //<---- ADD THIS LINE
cmd.CommandText = "insert into Employee_History (username, DateLogin) values (?,?);";
cmd.Parameters.AddWithValue("@username", username.Text);
cmd.Parameters.AddWithValue("@DateLogin", DateTime.Now.ToString("MM/dd/yyyy HH:mm"));
cmd.ExecuteNonQuery();

我鼓励您在查询中使用参数,我建议您以明文形式存储密码作为下一个练习。