轻量级ORM框架Dapper应用之实现In操作
时间:2022-11-13 10:14:18|栏目:.NET代码|点击: 次
IN 操作符允许我们在 WHERE 子句中规定多个值。
本篇文章中,还是使用和上篇文章中同样的实体类和数据库,Dapper使用in操作符的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using Dapper; using System.Data.SqlClient; using System.Data; using DapperApplicationByIn.Model; namespace DapperApplicationByIn { class Program { static void Main(string[] args) { // 定义连接字符串 string conn = ConfigurationManager.ConnectionStrings["AppConnection"].ConnectionString; #region in查询 using (IDbConnection connection = new SqlConnection(conn)) { var sql = "select * from Users where Email in @emails"; var result = connection.Query<User>(sql, new { emails = new string[2] { "fqy@qq.com", "hyj@163.com" } }); result.AsList().ForEach(p => { Console.WriteLine("Id:"+p.UserId+" UserName:"+p.UserName+" Email:"+p.Email+" Address:"+p.Address); }); } #endregion Console.ReadKey(); } } }
程序运行结果:
示例代码下载地址:点此下载
栏 目:.NET代码
本文地址:http://www.codeinn.net/misctech/218876.html