Dapper 查询
查询
获取查询结果的单个值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [HttpGet( "Count" )] public async Task<ActionResult> GetCount() { using IDbConnection con = new SqlConnection(MyConfig.ConnectionString); string sql = "select count(*) from T_Book " ; var count = await con.ExecuteScalarAsync(sql); // 返回dynamic类型 return Ok(count); } [HttpGet( "CountInt" )] public async Task<ActionResult> GetCountInt() { using IDbConnection con = new SqlConnection(MyConfig.ConnectionString); string sql = "select count(*) from T_Book" ; int count = await con.ExecuteScalarAsync< int >(sql); //返回整形 return Ok(count); } |
选择一行数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [HttpGet( "Product" )] public async Task<ActionResult> Query() { string sql = "select * from products where productid=1" ; using (IDbConnection con = new SqlConnection(MyConfig.ConnectionString)) { var product = await con.QuerySingleAsync(sql); return Ok(product); } } [HttpGet( "ProductType" )] public async Task<ActionResult> QueryType() { string sql = "select * from products where productid=1" ; using (IDbConnection con = new SqlConnection(MyConfig.ConnectionString)) { var product = await con.QuerySingleAsync<Product>(sql); return Ok(product); } } |
选择多行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [HttpGet( "customers" )] public async Task<ActionResult> Query() { string sql = "select * from customers" ; using (IDbConnection con = new SqlConnection(MyConfig.ConnectionString)) { var customers = await con.QueryAsync(sql); return Ok(customers); } } [HttpGet( "customersType" )] public async Task<ActionResult> QueryType() { var sql = "select * from customers" ; using (IDbConnection con = new SqlConnection(MyConfig.ConnectionString)) { var customers = await con.QueryAsync<Customer>(sql); return Ok(customers); } } |
INSERT
UPDATE
DELETE 插入、修改、删除 ,返回影响的行数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [HttpPost] public async Task<ActionResult> Insert() { var sql = "insert into categories (categoryName) values ('New Category')" ; using (IDbConnection con = new SqlConnection(MyConfig.ConnectionString)) { var affectedRows = await con.ExecuteAsync(sql); return Ok(affectedRows); } } [HttpPut] public async Task<ActionResult> Update() { var sql = @"update products set unitprice=unitprice* 0.1 where categoryid=2" ; using (IDbConnection con = new SqlConnection(MyConfig.ConnectionString)) { var rows = await con.ExecuteAsync(sql); return Ok(rows); } } [HttpDelete] public async Task<ActionResult> Delete() { var sql = "delete from categories where categoryName='New Category'" ; using (IDbConnection conn = new SqlConnection(MyConfig.ConnectionString)) { var rows = await conn.ExecuteAsync(sql); return Ok(rows); } } |
where in查詢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static string Libshp_PonoCid = "Select * from Libshp_PonoCid Where cid in @cid " ; private async Task<List<LibshpPonoCid>> GetListSource( string cids) { if ( string .IsNullOrEmpty(cids)) { return new List<LibshpPonoCid>(); } using IDbConnection conn = new SqlConnection(DbHelper.XFMesConnection); conn.Open(); var mycids = cids.Split( ',' ).ToArray(); var list = await conn.QueryAsync<LibshpPonoCid>(SqlHelper.Libshp_PonoCid, new { cid = mycids }); list ??= new List<LibshpPonoCid>(); return list.ToList(); } |
同時查詢兩段SQL,查詢回來一樣可以選擇是否要用強型別去接值。
1 2 3 4 5 6 7 8 9 10 11 12 | //QueryMultiple using (SqlConnection conn = new SqlConnection(strConnection)) { string strSql = "Select * from Users; Select * from Account;" ; using ( var results = conn. QueryMultiple(strSql)) { //第一段SQL var users = results.Read().ToList(); //第二段SQL 強型別 var accounts = results.Read<MyModel>().ToList(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报