zoukankan      html  css  js  c++  java
  • Dapper 返回Sql server 自增长ID 标识列SCOPE_IDENTITY

    原理

    使用SELECT SCOPE_IDENTITY(),取获取刚刚插入记录自增的主键

    示例

    entity.Create();
    StringBuilder strSql = new StringBuilder();
    strSql.Append(" insert into Attachment( ");
    strSql.Append(" [Code],[Name],[Path],[FileName],[SmallImageFileName],[CateID],[Size],[SizeUnit],[UserID],[SiteID],[Check],[InsertTime],[ModifyTime]) ");
    strSql.Append(" values ( ");
    strSql.Append(" @Code,@Name,@Path,@FileName,@SmallImageFileName,@CateID,@Size,@SizeUnit,@UserID,@SiteID,@Check,@InsertTime,@ModifyTime); ");
    strSql.Append(" SELECT CAST(SCOPE_IDENTITY() as int) ");
    
    var dp = new DynamicParameters(new { });
    dp.Add("Code", entity.Code, DbType.Int32);
    dp.Add("Name", entity.Name, DbType.String);
    dp.Add("Path", entity.Path, DbType.String);
    dp.Add("FileName", entity.FileName, DbType.String);
    dp.Add("SmallImageFileName", entity.SmallImageFileName, DbType.String);
    dp.Add("CateID", entity.CateID, DbType.Int32);
    dp.Add("Size", entity.Size, DbType.Int32);
    dp.Add("SizeUnit", entity.SizeUnit, DbType.Int16);
    dp.Add("UserID", entity.UserID, DbType.Int32);
    dp.Add("SiteID", entity.SiteID, DbType.Int32);
    dp.Add("Check", entity.Check, DbType.Int16);
    dp.Add("InsertTime", entity.InsertTime, DbType.DateTime);
    dp.Add("ModifyTime", entity.ModifyTime, DbType.DateTime);
    
    return this.BaseRepository().FindObject(strSql.ToString(), dp).ToInt();
  • 相关阅读:
    静态类、抽象类的笔记
    第一篇
    流复制操作
    速记服务器状态码
    访问修饰符
    jquery对象、js全局变量等tips
    Caching and Indexing
    Using X++ copy the favorites from one user to another
    SID
    Using X++ get Language List from Dynamics AX 2009
  • 原文地址:https://www.cnblogs.com/masonblog/p/12726495.html
Copyright © 2011-2022 走看看