1. 程式人生 > >Dapper官方教程翻譯2:Dapper方法之Execute

Dapper官方教程翻譯2:Dapper方法之Execute

Execute方法描述:

 

Execute是Dapper對資料庫操作的一個擴充套件,可以由IDbConnection物件呼叫。它可以執行一條命令一或多次,返回型別是受影響的行數。這個方法通常用於執行:

 

該方法可傳遞的引數:

 

Execute方法引數說明
引數名 引數含義
Sql 可執行的資料庫語句
param 命令中的佔位引數
transaction 使用的事務
commandTimeout 超時時長
commandType 命令型別

 

示例:執行儲存過程

 

執行一次儲存過程:

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

執行多次儲存過程:

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

 

示例:Execute執行插入語句

 

執行單條插入:

string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	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);
}

執行多條插入:

string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();

	var affectedRows = connection.Execute(sql,
	new[]
	{
	new {CustomerName = "John"},
	new {CustomerName = "Andy"},
	new {CustomerName = "Allan"}
	}
);

Console.WriteLine(affectedRows);

 

示例:Execute執行更新語句

 

執行單條更新:

string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{			
	var affectedRows = connection.Execute(sql,new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"});

	Console.WriteLine(affectedRows);
}

執行多條更新:

string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{	
	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);

 

示例:Execute執行刪除操作

 

執行單條刪除:

string sql = "DELETE FROM Customers WHERE CustomerID = @CustomerID";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{			
	var affectedRows = connection.Execute(sql, new {CustomerID = 1});

	Console.WriteLine(affectedRows);
}

執行多條刪除:

string sql = "DELETE FROM OrderDetails WHERE OrderDetailID = @OrderDetailID";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{			
	var affectedRows = connection.Execute(sql, 
		new[]
	{
	new {OrderDetailID = 1},
	new {OrderDetailID = 2},
	new {OrderDetailID = 3}
	}
);

Console.WriteLine(affectedRows);