sqlserver有output,Oracle有Sequence。Access用下面的方法,注意两次查询用一个connection:
public int InsertEx(User user) { int id = -1; using (OleDbConnection conn = new OleDbConnection(AccessHelper.connectionString)) { conn.Open(); OleDbCommand cmd = new OleDbCommand { Connection = conn, CommandType = CommandType.Text }; string commandText = $"insert into tb_user (username,userpassword,insdate,dr) values (@username, @userpwd, Date(), 0)"; List<OleDbParameter> paras = new List<OleDbParameter> { new OleDbParameter("username", user.UserName), new OleDbParameter("userpwd", user.UserPassword) }; cmd.CommandText = commandText; cmd.Parameters.AddRange(paras.ToArray()); cmd.ExecuteNonQuery(); cmd.CommandText = "select @@identity as id"; cmd.Parameters.Clear(); id = Convert.ToInt32(cmd.ExecuteScalar()); } return id; }
转 : https://www.cnblogs.com/David-Huang/p/6180179.html
原文:https://www.cnblogs.com/fps2tao/p/14692713.html