使用MVC 2,MySQL
public ActionResult Register() { string name = Request.Form["name"]; string password = Request.Form["password"]; ViewData["name"] = name; ViewData["password"] = password; // Pasa los datos al ViewData, hace el insert MySqlCommand cmd = new MySqlCommand("INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')", cn); try { // Intenta conectarse e insertar los datos cn.Open(); cmd.ExecuteNonQuery(); return View("~/Views/Inicial/Inicial.aspx"); } catch (Exception ex) { // si no puede, error ViewData["ex"] = ex; System.Diagnostics.Debug.WriteLine(ex); return View("Error"); } }这是我的控制器代码我的错误代码是:
Lo sentimos; se produjo un error al procesar la solicitud。 MySql.Data.MySqlClient.MySqlException(0x80004005):在MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&affectedRow,Int64&insertedId)的MySql.Data.MySqlClient.MySqlStream.ReadPacket()处为密钥'PRIMARY'复制条目''在MySql的MySql.Data.MySqlClient.MySqlDataReader.NextResult()的MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,Boolean force)中的MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&affectedRows,Int64&insertedId)位于E:\的MvcApplication1.Controllers.ValidarController.Register()的MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()的MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()处的.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) Proyecto \ TestsDByAjax \ MvcApplication1 \ Controllers \ ValidarController.cs:第61行
第61行说cmd.ExecuteNonQuery()
Using MVC 2 , MySQL
public ActionResult Register() { string name = Request.Form["name"]; string password = Request.Form["password"]; ViewData["name"] = name; ViewData["password"] = password; // Pasa los datos al ViewData, hace el insert MySqlCommand cmd = new MySqlCommand("INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')", cn); try { // Intenta conectarse e insertar los datos cn.Open(); cmd.ExecuteNonQuery(); return View("~/Views/Inicial/Inicial.aspx"); } catch (Exception ex) { // si no puede, error ViewData["ex"] = ex; System.Diagnostics.Debug.WriteLine(ex); return View("Error"); } }This is my code from the controller And my error code is:
Lo sentimos; se produjo un error al procesar la solicitud. MySql.Data.MySqlClient.MySqlException (0x80004005): Duplicate entry '' for key 'PRIMARY' at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at MvcApplication1.Controllers.ValidarController.Register() in E:\Proyecto\TestsDByAjax\MvcApplication1\Controllers\ValidarController.cs:line 61
Line 61 says cmd.ExecuteNonQuery()
最满意答案
我的意思是,我已经有了一个错误的答案,我只是不知道你想用它做什么?
"INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')"在上面的语句中,您将名称和密码插入表中。 并且你声称另一个字段,即DNI是桌面上的主键。 你显然不会在DNI插入任何内容。 如果字段DNI具有标识插入,则您不会对上述语句产生问题。 但你可能没有,这就是它抱怨的原因。 因此,如果您提供唯一的DNI ,那么您可以
I mean, I have an answer already what is wrong, I just don't know what you want to do with it?
"INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')"In the statement above you inserting name and password into table. And you claiming that another field, namely DNI is the primary key on the table. You obviously don't insert anything into DNI. If the field DNI had identity insert you wouldn't have issue with the statement above. But you probably don't and this is why it complaining. So, if you would supply unique DNI, you would be OK
更多推荐
发布评论