Execute

2022-04-21 09:42 更新

描述

Execute 是一個(gè)可以從 IDbConnection 類型的任意對(duì)象調(diào)用的擴(kuò)展方法,它可以執(zhí)行一個(gè)或多個(gè)命令并返回受影響的行數(shù)。此方法通常用于執(zhí)行:

  • 存儲(chǔ)過程
  • INSERT 語句
  • UPDATE 語句
  • DELETE 語句

參數(shù)

下表顯示了 Execute 方法的不同參數(shù)。

名稱 描述
sql 要執(zhí)行的命令文本。
param 命令參數(shù)(默認(rèn)為null)。
transaction 需要使用的事務(wù)(默認(rèn)為null)。
commandTimeout 命令執(zhí)行超時(shí)時(shí)間(默認(rèn)為null)。
commandType 命令類型(默認(rèn)為null)。

案例 - 執(zhí)行存儲(chǔ)過程

單次

執(zhí)行一次存儲(chǔ)過程。

string sql = "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);
}

多次

執(zhí)行多次存儲(chǔ)過程,為參數(shù)數(shù)組列表中的每個(gè)對(duì)象執(zhí)行一次。

string sql = "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);
}

案例 - 執(zhí)行 INSERT 語句

單次

執(zhí)行一次 INSERT 語句。

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);
}

多次

執(zhí)行 INSERT 語句,為參數(shù)數(shù)組列表中的每個(gè)對(duì)象執(zhí)行一次。

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);
}

案例 - 執(zhí)行 UPDATE 語句

單次

執(zhí)行一次 UPDATE 語句。

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);
}

多次

執(zhí)行 UPDATE 語句,為參數(shù)數(shù)組列表中的每個(gè)對(duì)象執(zhí)行一次。

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);
}

案例 - 執(zhí)行 DELETE 語句

單次

執(zhí)行一次 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);
}

多次

執(zhí)行 DELETE 語句,為參數(shù)數(shù)組列表中的每個(gè)對(duì)象執(zhí)行一次。

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}
        });
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)