Execute
是一个可以从IDbConnection
类型的任意对象调用的扩展方法,它可以执行一个或多个命令并返回受影响的行数。此方法通常用于执行:
下表显示了Execute
方法的不同参数。
名称 | 描述 |
---|---|
sql | 要执行的命令文本。 |
param | 命令参数(默认为null )。 |
transaction | 需要使用的事务(默认为null )。 |
commandTimeout | 命令执行超时时间(默认为null )。 |
commandType | 命令类型(默认为null )。 |
执行一次存储过程。
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);
}
执行多次存储过程,为参数数组列表中的每个对象执行一次。
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);
}
执行一次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);
}
执行INSERT
语句,为参数数组列表中的每个对象执行一次。
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);
}
执行一次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);
}
执行UPDATE
语句,为参数数组列表中的每个对象执行一次。
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);
}
执行一次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);
}
执行DELETE
语句,为参数数组列表中的每个对象执行一次。
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}
});
}