sqlserver有output,Oracle有Sequence。Access用下面的方法,注意两次查询用一个connection:
public int InsertEx(User user)
{
int id = -; 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;
}