相同的记录被插入两次

时间:2015-03-09 11:13:46

标签: mysql asp.net-mvc

首先插入相同的记录6次,但现在插入两次,我没有改变一件事。无论如何,我只希望它只插入一次。我的代码如下所示:

public class DBLogic
    {
    internal bool RegisterNewUser(string uName, string email, string password)
        {            
            if (EmailAlreadyExists(email))
            {
                return false;
            }

            DateTime today = DateTime.Now;
            DateTime dateOfReg = today; // today.ToString("dd/MM/yyyy");

            using (_msqlCon = new MySqlConnection(_connectionString))
            {
                _msqlCon.Open();

                _query = "INSERT INTO RegisteredUsers (Username, Email, Password, DateOfRegistration) VALUES (?uName, ?email, ?password, ?dateOfReg)";
                _command = new MySqlCommand(_query, _msqlCon);

                _command.Parameters.AddWithValue("uName", uName);
                _command.Parameters.AddWithValue("email", email);
                _command.Parameters.AddWithValue("password", password);
                _command.Parameters.AddWithValue("dateOfReg", dateOfReg);

                _command.ExecuteNonQuery();
            }

            return true;
        }
}

在开始时我没有使用using语句,然后它似乎工作正常。虽然我不确定这是using声明是否有罪。您能否在我的代码中看到可能导致问题的任何问题?

启动调用的代码是Action方法:

[HttpPost]
    public ActionResult RegisterNewUser(RegisteredUsersModel registerUserModel)
    {
        ViewBag.Header = "Register";
        string msg = "";

        if (ModelState.IsValid)
        {
            if (!registerUserModel.RegisterUser(registerUserModel.nickName, registerUserModel.email, registerUserModel.password))
            {
                msg = "The provided email already exists";
            }

            ViewBag.DeniedMsg = msg;
            return PartialView("_Register");
        }

        return PartialView("_Register");
    }

动作方法将其发送到Model:

    namespace WebApplication1.Models
   {
    public RegisteredUsersModel()
        {
            logic = new DBLogic();
        }

    public bool RegisterUser(string uName, string email, string password)
        {
            return logic.RegisterNewUser(uName, email, password);
        }
   }

逻辑类是上面插入数据库的逻辑类 提前致谢

0 个答案:

没有答案