C# 数据操作系列 - 17 Dapper ——号称可以与ADO.NET 同台飙车的ORM (2)

看一下它的基本参数和方法声明:

public static GridReader QueryMultiple(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

这个方法返回一个GridReader,通过Read方法获取需要的数据。

5. 不只是查询

Dapper当然不只有查询这一项功能,Dapper支持使用存储过程、insert、update、delete等其他的SQL语句进行操作数据库。使用方式:

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); var affectedRows2 = 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 ); }

示例就是使用存储过程的例子。

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); } //== 多次插入 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"} } );

这是执行插入的示例。

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

更新。

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

删除。

Execute没什么好说的,基本就是执行SQL语句的形式完成增删改成等操作。

值得注意的是:

public static IDataReader ExecuteReader(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

返回一个IDataReader实例,这个实例可以给DataTable填充数据,使用方法如下:

DataTable table = new DataTable(); table.Load(reader);

以及:

public static T ExecuteScalar<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

这个方法是返回查询结果的第一行第一列的元素。

6. 总结

如果单说Dapper的话,并没有太多好说的。不过Dapper是真的快,在实际开发中有时候会用Dapper作为EF Core的一个补充。

当然了,Dapper还有很多其他的插件,使用那些插件可以为Dappe带来非一般的提升。我们下一篇将介绍一下Dapper的插件。

更多内容烦请关注我的博客《高先生小屋》

file

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppjfx.html