zoukankan      html  css  js  c++  java
  • Access获取新插入数据的自增长主键Id

    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

  • 相关阅读:
    ActiveMQ简单介绍+简单实例
    分布式开放消息系统(RocketMQ)的原理与实践
    RocketMQ实战(一)
    十分钟入门RocketMQ
    android Lib
    JDK1.8 Lambda
    JDK1.8聚合操作
    JNI
    Gradle插件
    Weex命令
  • 原文地址:https://www.cnblogs.com/fps2tao/p/14692713.html
Copyright © 2011-2022 走看看