Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch insert #32

Closed
54Ezreal opened this issue Aug 23, 2018 · 2 comments
Closed

batch insert #32

54Ezreal opened this issue Aug 23, 2018 · 2 comments

Comments

@54Ezreal
Copy link

请问批量添加功能如何实现呢,我参照文档的 这样子的写法,这样子会有异常抛出,提示For 的Key 键不能为空!

INSERT INTO T_Test
(Name,Status)
VALUES

(@name,@Status)


我觉得批量添加在开发中还是会经常用到,比方说做Excel导入时,订单项添加时,都会用到批量添加,希望作者能够写一些示例。我查阅了SmartSqlStarter项目,内容较少也没有涉及到批量操作的功能。
Thanks

@Ahoo-Wang
Copy link
Member

@HuUncle

    <Statement Id="InsertBatch">
      INSERT INTO T_Entity
      (FString
      ,FDecimal
      ,FNullDecimal
      ,FBool
      ,FNullBool
      ,Status
      ,NullStatus
      ,CreationTime
      ,LastUpdateTime)
      VALUES
      <For Open="" Close="" Key="T" Property="Items" Separator=",">
        (@FString
        ,@FDecimal
        ,@FNullDecimal
        ,@FBool
        ,@FNullBool
        ,@Status
        ,@NullStatus
        ,@CreationTime
        ,@LastUpdateTime)
      </For>
    </Statement>
            var items = new List<T_Entity> {
                new T_Entity
                {
                    CreationTime = DateTime.Now,
                    FBool = true,
                    FDecimal = 1,
                    FLong = 1,
                    FNullBool = false,
                    FString = Guid.NewGuid().ToString("N"),
                    FNullDecimal = 1.1M,
                    LastUpdateTime = DateTime.Now,
                    Status = EntityStatus.Ok
                },new T_Entity
                {
                    CreationTime = DateTime.Now,
                    FBool = true,
                    FDecimal = 1,
                    FLong = 1,
                    FNullBool = false,
                    FString = Guid.NewGuid().ToString("N"),
                    FNullDecimal = 1.1M,
                    LastUpdateTime = DateTime.Now,
                    Status = EntityStatus.Ok
                }
            };
            _sqlMapper.Execute(new RequestContext
            {
                Scope = Scope,
                SqlId = "InsertBatch",
                Request = new { Items = items }
            });

@ghd258
Copy link

ghd258 commented Aug 23, 2018

方法不错

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants