如果获取LAST_INSERT_ID()数据
我原来用的是mysql5.5.28版本,下面的函数能够正常调用,现在我把数据库改为5.6.12免安装版,出现了“指定的转换无效”的错误,试了几种类型也都不行,问下该如何获取该数据,谢谢!
函数内容介绍:插入一行用户信息,userid为自增类型数据,插入后通过LAST_INSERT_ID()获取刚插入的值并返回
public bool InsertRowUserInfo(DataRow rowUserInfo, out UInt32 userIdxx)
{
DataTable tbId = new DataTable();
userIdxx = 0;
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandText = "InsertTbUserInfo";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("?p_userid", rowUserInfo["USER_ID"]);
command.Parameters.AddWithValue("?p_username", rowUserInfo["USER_NAME"]);
command.Parameters.AddWithValue("?p_phone", rowUserInfo["PHONE"]);
command.Parameters.AddWithValue("?p_address", rowUserInfo["ADDRESS"]);
command.Parameters.AddWithValue("?p_usermemo", rowUserInfo["USER_MEMO"]);
command.Parameters.AddWithValue("?p_villageid", rowUserInfo["VILLAGE_ID"]);
command.ExecuteNonQuery();
//通过SELECT LAST_INSERT_ID(); 获取最近插入的自增型数据的值。
command.CommandText = "SelectLastInsertID"; //即LAST_INSERT_ID()
command.ExecuteNonQuery();
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
tbId.Clear();
adapter.Fill(tbId);
connection.Close();
userIdxx = (UInt32)(long)tbId.Rows[0][0];
command.Dispose();
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
return true;
}