首页 > 数据库技术 > 详细

Access获取新插入数据的自增长主键Id

时间:2021-04-23 16:22:10      阅读:18      评论:0      收藏:0      [点我收藏+]

 

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

 

Access获取新插入数据的自增长主键Id

原文:https://www.cnblogs.com/fps2tao/p/14692713.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!