正在接收 "Duplicate enty for primary key"
Receiving "Duplicate enty for primary key"
使用 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): 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
第 61 行说 cmd.ExecuteNonQuery()
我的意思是,我已经知道哪里出了问题,我只是不知道你想用它做什么?
"INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')"
在上面的语句中,您将名称和密码插入 table。而您声称另一个字段 DNI
是 table 上的主键。您显然没有向 DNI
中插入任何内容。如果字段 DNI
有标识插入,则上面的语句不会有问题。但是您可能不这样做,这就是它抱怨的原因。所以,如果你提供独特的 DNI
,你就可以
使用 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): 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
第 61 行说 cmd.ExecuteNonQuery()
我的意思是,我已经知道哪里出了问题,我只是不知道你想用它做什么?
"INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')"
在上面的语句中,您将名称和密码插入 table。而您声称另一个字段 DNI
是 table 上的主键。您显然没有向 DNI
中插入任何内容。如果字段 DNI
有标识插入,则上面的语句不会有问题。但是您可能不这样做,这就是它抱怨的原因。所以,如果你提供独特的 DNI
,你就可以