描述
Execute是一種擴(kuò)展方法,可以從IDbConnection類型的任何對(duì)象調(diào)用。它可以執(zhí)行一次或多次命令并返回受影響的行數(shù)。此方法通常用于執(zhí)行。
- 存儲(chǔ)過(guò)程
- Insert語(yǔ)句
- Update 語(yǔ)句
- Delete 語(yǔ)句
參數(shù)
下表顯示了Execute方法的不同參數(shù)。
名稱 描述
- SQL 要執(zhí)行的命令文本。
- PARAM 命令參數(shù)(default = null)。
- DbTransaction 要使用的事務(wù)(default = null)。
- CommandTimeout 命令超時(shí)(默認(rèn)= null)
命令類型 命令類型(默認(rèn)= null)
示例:執(zhí)行存儲(chǔ)過(guò)程
- 單對(duì)象操作
string sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
var affectedRows = connection.Execute(sql,
new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
commandType: CommandType.StoredProcedure);
My.Result.Show(affectedRows);
}
- 多對(duì)象操作
多次執(zhí)行存儲(chǔ)過(guò)程。一次為數(shù)組列表中的每個(gè)對(duì)象。
string sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
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í)行單條信息插入
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});
Console.WriteLine(affectedRows);
var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
FiddleHelper.WriteTable(customer);
}
多次執(zhí)行INSERT語(yǔ)句。一次為數(shù)組列表中的每個(gè)對(duì)象。
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.Open();
var affectedRows = connection.Execute(sql,
new[]
{
new {CustomerName = "John"},
new {CustomerName = "Andy"},
new {CustomerName = "Allan"}
}
);
Console.WriteLine(affectedRows);
- 示例:Update
執(zhí)行一條信息更新
string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql,new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"});
Console.WriteLine(affectedRows);
}
執(zhí)行多條信息更新,一次為數(shù)組列表中的每個(gè)對(duì)象。
string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql,
new[]
{
new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"},
new {CategoryID = 4, Description = "Cheeses and butters etc."}
}
);
Console.WriteLine(affectedRows);
- 示例:Delete
執(zhí)行單條Delete語(yǔ)句
string sql = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql, new {CustomerID = 1});
Console.WriteLine(affectedRows);
}
執(zhí)行DELETE語(yǔ)句。一次為數(shù)組列表中的每個(gè)對(duì)象
string sql = "DELETE FROM OrderDetails WHERE OrderDetailID = @OrderDetailID";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql,
new[]
{
new {OrderDetailID = 1},
new {OrderDetailID = 2},
new {OrderDetailID = 3}
}
);
Console.WriteLine(affectedRows);