Execute 是一個(gè)可以從 IDbConnection 類型的任意對(duì)象調(diào)用的擴(kuò)展方法,它可以執(zhí)行一個(gè)或多個(gè)命令并返回受影響的行數(shù)。此方法通常用于執(zhí)行:
下表顯示了 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ǔ)過程。
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 語句。
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 語句。
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 語句。
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}
});
}
更多建議: