我正在尝试创建一种方法,可以在其中执行 mysql UPDATE、DELETE 或 INSERT 查询。该方法必须在使用我询问或不询问的 INSERT 时起作用last_insert_id()
。以下是我目前拥有的代码:
public int executeUID(MySqlCommand msCommand)
{
try
{
this.Open();
msCommand.Connection = this.msCon;
return int.Parse(msCommand.ExecuteScalar().ToString());
}
catch (MySqlException ex)
{
throw ex;
}
finally
{
this.Close();
}
}
这样做的问题是,当我使用返回 a 的插入查询时,last_insert_id()
该方法效果很好。但是当查询不返回时last_insert_id()
,方法就会出现故障。我怎样才能让这个方法起作用?