Dapper入门教程——执行非查询语句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dapper入门教程——执行非查询语句相关的知识,希望对你有一定的参考价值。

描述

你可以从任意实现IDbConnection的类对象中调用Dapper的扩展方法“Execute”。它能够执行一条命令(Command)一次或者多次,并返回受影响的行数。这个方法通常用来执行:

  • Stored Procedure
  • INSERT statement
  • UPDATE statement
  • DELETE statement

参数

下面表格中显示了Execute方法的不同参数

 

名称描述
sql

要执行的sql语句文本

param

command的参数

transaction

事务

commandTimeout

command超时时间

commandType

command类型

示例:执行存储过程

单次

执行存储过程一次

string sql = "EXEC Invoice_Insert";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql,
        new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
        commandType: CommandType.StoredProcedure);

    My.Result.Show(affectedRows);
}

技术分享

多次

执行存储过程多次,数组中的每条数据都执行一次

string sql = "EXEC Invoice_Insert";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql,
        new[]
        {
            new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_1"},
            new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_2"},
            new {Kind = InvoiceKind.StoreInvoice, Code = "Many_Insert_3"}
        },
        commandType: CommandType.StoredProcedure
    );

    My.Result.Show(affectedRows);
}

示例:执行插入

单次

执行插入语句一次

 

string sql = "INSERT INTO Invoice (Code) Values (@Code);";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql, new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"});

    My.Result.Show(affectedRows);
}

多次

执行多次,数组中的每条数据都执行一次

 

string sql = "INSERT INTO Invoice (Code) Values (@Code);";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql,
        new[]
        {
            new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_1"},
            new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_2"},
            new {Kind = InvoiceKind.StoreInvoice, Code = "Many_Insert_3"}
        }
    );

    My.Result.Show(affectedRows);
}

示例:执行Update

单次

执行Update语句一次

string sql = "UPDATE Invoice SET Code = @Code WHERE InvoiceID = @InvoiceID";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql, new {InvoiceID = 1, Code = "Single_Update_1"});

    My.Result.Show(affectedRows);
}

多次

执行多次,数组中的每条数据都执行一次

string sql = "UPDATE Invoice SET Code = @Code WHERE InvoiceID = @InvoiceID";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql,
        new[]
        {
            new {InvoiceID = 1, Code = "Many_Update_1"},
            new {InvoiceID = 2, Code = "Many_Update_2"},
            new {InvoiceID = 3, Code = "Many_Update_3"}
        });

    My.Result.Show(affectedRows);
}

示例:执行Delete

单次

执行Delete语句一次

string sql = "DELETE FROM Invoice WHERE InvoiceID = @InvoiceID";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql, new {InvoiceID = 1});

    My.Result.Show(affectedRows);
}

多次

执行多次,数组中的每条数据都执行一次

string sql = "DELETE FROM Invoice WHERE InvoiceID = @InvoiceID";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var affectedRows = connection.Execute(sql,
        new[]
        {
            new {InvoiceID = 1},
            new {InvoiceID = 2},
            new {InvoiceID = 3}
        });
}

 

 

 

 

 

以上是关于Dapper入门教程——执行非查询语句的主要内容,如果未能解决你的问题,请参考以下文章

Dapper入门教程——Dapper Query查询

查看要在Dapper中执行的SQL语句

Dapper:使用 <T> 执行来自不同资源的多个查询

Dapper系列 作者:懒懒的程序员一枚

基于Dapper的分页实现,支持筛选,排序,结果集总数,非存储过程

为啥实体框架在直接选择语句中的执行速度比 Dapper 快 [关闭]