【发布时间】:2011-04-04 20:11:37
【问题描述】:
与此帖子相关的问题,我认为他处于离线状态,因此我无法在 atm 获得任何反馈: MySql and inserting last ID problem remains 我不确定我是否完全失明,但在这段代码中:
// run the insert using a non query call
command.CommandText = cmd.ToString();
command.ExecuteNonQuery();
/* now we want to make a second call to MYSQL to get the new index
value it created for the primary key. This is called using scalar so it will
return the value of the SQL statement. We convert that to an int for later use.*/
command.CommandText = "select last_insert_id();";
id = Convert.ToInt32(command.ExecuteScalar());
//------- the name id does not exist in the current context
// Commit the transaction.
transaction.Commit();
}
catch (Exception ex)
{
Label10.Text = ": " + ex.Message;
try
{
// Attempt to roll back the transaction.
transaction.Rollback();
}
catch
{
// Do nothing here; transaction is not active.
}
}
}
id 没有设置任何东西,我不确定他是如何尝试将其设置为最后插入的 id?
编辑:
int id = Convert.ToInt32(cmd.ExecuteScalar());
Label10.Text = Convert.ToString(id);
【问题讨论】: