Dapper Tutorial Dapper - 실행
기술
Execute는 IDbConnection 유형의 모든 객체에서 호출 할 수있는 확장 메서드입니다. 명령을 한 번 또는 여러 번 실행하고 영향을받은 행 수를 반환 할 수 있습니다. 이 메소드는 대개 다음을 실행하는 데 사용됩니다.
매개 변수
다음 표는 Execute 메서드의 다른 매개 변수를 보여줍니다.
이름 | 기술 |
---|---|
SQL | 실행할 명령 텍스트. |
매개 변수 | 명령 매개 변수 (기본값 = null). |
트랜잭션 | 사용할 트랜잭션 (기본값 = null)입니다. |
commandTimeout | 명령 timeout (기본값 = null) |
commandType | 명령 유형 (기본값 = null) |
예 - 저장 프로 시저 실행
단일
저장 프로 시저를 한 번만 실행하십시오.
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); }
예 - INSERT 실행
단일
한 번에 INSERT 문을 실행하십시오.
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); }
include component-try-it.html href = 'https : //dotnetfiddle.net/P2uw27'%}
많은
INSERT 문을 여러 번 실행하십시오. 배열 목록에있는 모든 객체에 대해 한 번.
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);
include component-try-it.html href = 'https : //dotnetfiddle.net/vHOVx6'%}
예 - UPDATE 실행
단일
UPDATE 문을 한 번만 실행하십시오.
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); }
include component-try-it.html href = 'https : //dotnetfiddle.net/CWdH6z'%}
많은
UPDATE 문을 여러 번 실행하십시오. 배열 목록에있는 모든 객체에 대해 한 번.
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);
include component-try-it.html href = 'https : //dotnetfiddle.net/qCdKI3'%}
예 - DELETE 실행
단일
DELETE 문을 한 번만 실행하십시오.
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); }
include component-try-it.html href = 'https : //dotnetfiddle.net/4bFT32'%}
많은
DELETE 문을 여러 번 실행하십시오. 배열 목록에있는 모든 객체에 대해 한 번.
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);
include component-try-it.html href = 'https : //dotnetfiddle.net/nxP1vL'%}