MySQL插入数据获得自动增长的ID
INSERT INTO data(name) Values(@name); SELECT @@Identity
/// <summary>
/// 插入一个用户返回自动增长的id
/// </summary>
/// <param name="userInfo"></param>
/// <returns></returns>
public int Add(UserInfo userInfo)
{
//string sql = "insert into " + JDBCConst.UserInfoDB + " value(NULL,@cardId,@username,@password,@role)";
// 插入一个用户返回自动增长的id
string sql = "insert into " + JDBCConst.UserInfoDB + " value(NULL,@cardId,@username,@password,@role,null);SELECT @@Identity";
MySqlParameter parCardId = new MySqlParameter("@CardId", userInfo.card_id);
MySqlParameter parUsername = new MySqlParameter("@username", userInfo.username);
MySqlParameter parPassword = new MySqlParameter("@password", userInfo.password);
MySqlParameter parRole = new MySqlParameter("@role", userInfo.role);
object i;
try
{
i = Utils.ExecuteScalar(sql,parCardId,parUsername,parPassword,parRole);
}
catch (Exception)
{
throw;
}
int k = Convert.ToInt32(i);
return k;
}
本文作者:大海&
本文链接:https://www.cnblogs.com/oceanus/p/14206350.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。