SQL数据库添加 返回ID

     /// <summary>
        /// 数据库添加 返回ID
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>返回查询表</returns>
        public static string set2(string sql)
        {
            string id = "";
            OleDbConnection oleDb = new OleDbConnection(oledbtext);
            oleDb.Open();
            OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDb);
            int i = oleDbCommand.ExecuteNonQuery(); //返回被修改的数目
            DataTable dt = new DataTable(); //新建表对象
            if (i>0)
            {
                OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter("SELECT @@IDENTITY AS 'Id'", oleDb); //创建适配对象            
                dbDataAdapter.Fill(dt); //用适配对象填充表对象
                if (dt.Rows.Count > 0)
                {
                    id = dt.Rows[0].ItemArray[0].ToString();
                }
            }
                       

            return id;

        }

 

posted @ 2021-03-26 21:51  學海無邊  阅读(419)  评论(0编辑  收藏  举报