Dapper基本增删改查
说明:
1、在using语句块中不用dbConnection.Open(),因为Execute方法中会Open,并且在执行完成会Close。
2、在Ado.Net中要手动Open,在Using语句块中使用时,可以不用手动Close。
public ActionResult Select()
{
string sql = @"select * from Info";
List<Info> list = null;
using (IDbConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
{
list = dbConnection.Query<Info>(sql, CommandType.StoredProcedure).ToList();
SqlMapper.GridReader gridReader = dbConnection.QueryMultiple(sql);
gridReader.Read();
} return Content("");
} public ActionResult Update()
{
string sql = @"update info set Name=@Name where Age=@Age";
using (IDbConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
{
dbConnection.Execute(sql, new Info() { Name = "s张", Age = });
}
return Content("");
} public ActionResult Delete()
{
string sql = @"delete from info where Age=@Age";
using (IDbConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
{
dbConnection.Execute(sql, new Info() { Age = });
}
return Content("OK");
} public ActionResult Insert()
{
string sql = "insert Userinfo values('zhao2323',23,'上海')";
string str = "";
IDbConnection dbConnection = null;
using (dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
{
str += "<br />Execute之前" + dbConnection.State;
dbConnection.Execute(sql);
str += "<br />Execute之后" + dbConnection.State;
}
str += "<br />using之后" + dbConnection.State;
return Content("OK" + str);
//执行结果:
// Execute之前Closed
//Execute之后Closed
//using之后Closed
} public ActionResult AdoNet()
{
string sql = "insert into UserInfo values('zhao',23,'上海')";
string str = "";
SqlConnection connection = null;
using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
{
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
command.ExecuteNonQuery();
str += "<br />ExecuteNonQuery之后:" + connection.State;
}
str += "<br />using Command之后:" + connection.State;
}
str += "<br />using Connection之后:" + connection.State;
return Content("Ok" + str);
//执行结果
// ExecuteNonQuery之后:Open
//using Command之后:Open
//using Connection之后:Closed
}
Dapper基本增删改查的更多相关文章
- 使用轻量级ORM Dapper进行增删改查
项目背景 前一段时间,开始做一个项目,在考虑数据访问层是考虑技术选型,考虑过原始的ADO.NET.微软的EF.NH等.再跟经理讨论后,经理强调不要用Ef,NH做ORM,后期的sql优化不好做,公司 ...
- Dapper.Contrib——更加优雅地使用Dapper进行增删改查
简介 Dapper是介于Entity framework与ADO的折中选择.既满足手写查询的高性能需求,又简化了数据库对象映射为内存对象的繁杂工作.Dapper.Contrib是对Dapper的进一步 ...
- Dapper进行增删改查 z
http://www.cnblogs.com/huangkaiyan10/p/4640548.html 项目背景 前一段时间,开始做一个项目,在考虑数据访问层是考虑技术选型,考虑过原始的ADO.NET ...
- .net Core+Dapper MySQL增删改查
新建一个用户表,以该有为例 1.Model层 public class TuiUsers { public int id { get; set; } public string userName { ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- 在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查
一.首先先创建一个C#的控制台应用 二.然后添加上必要的命名空间 using System;using System.Collections.Generic;using MySql.Data.MySq ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 自定义仓储之增删改查
上一篇文章(https://www.cnblogs.com/meowv/p/12913676.html)我们用Code-First的方式创建了博客所需的实体类,生成了数据库表,完成了对EF Core的 ...
- “造轮运动”之 ORM框架系列(一)~谈谈我在实际业务中的增删改查
想想毕业已经快一年了,也就是大约两年以前,怀着满腔的热血正式跨入程序员的世界,那时候的自己想象着所热爱的技术生涯会是多么的丰富多彩,每天可以与大佬们坐在一起讨论解决各种牛逼的技术问题,喝着咖啡,翘着二 ...
- ORM增删改查并发性能测试
这两天在对一些ORM进行性能测试(涉及SqlSugar.FreeSql.Fast.Framework.Dapper.LiteSql),测试用的是Winform程序,别人第一眼看到我的程序,说,你这测试 ...
随机推荐
- The Managed Metadata Service or Connection is currently not available
Does the following error message looks familiar to you? when you go to site Actions -> Site Sett ...
- setNeedsDisplay 和 setNeedsLayout-b
setNeedsDisplay: Marks the receiver’s entire bounds rectangle as needing to be redrawn. drawRect: Th ...
- Android Service之LOCATION_SERVICE
Android提供了GPS功能 LocationManager obj = (LocationManager)getSystemService(Context.LOCATION_SERVICE) Pe ...
- LED驅動芯片 兩種恒流控制方式
下面要說的是,兩種恒流控制模式的開關電源,從而產生兩種做法.這兩種做法無論是原理,還是器件應用,還是性能差別,相當都較大. 首先說原理.第一種以現在恒流型LED專用IC為代表,主要如9910系 ...
- 【宽搜】【并查集】Vijos P1015 十字绣
题目链接: https://vijos.org/p/1015 题目大意: n*m的网格,线只能在网格的顶点处才能从布的一面穿到另一面.每一段线都覆盖一个单位网格的两条对角线之一,而在绣的过程中,一针中 ...
- HDOJ(HDU) 2060 Snooker(英语很重要。。。)
Problem Description background: Philip likes to play the QQ game of Snooker when he wants a relax, t ...
- 【2】JAVA---地址App小软件(ListPanel.class)(表现层)
这个是表现层的主界面. /* * ListPanel.java * */ package cn.hncu.addr.ui; import javax.swing.JFrame; import java ...
- 本地存储-webStorage
webStorage 提供了一种方式让网站能够把信息存储到你本地的计算机上,并在以后需要的时候进行获取.这个概念和cookie相似,区别是它是为了更大容量存储设计的.Cookie的大小是受限的,并且每 ...
- div+css 圆角加阴影
.test{ display: inline-block; padding: 5px 10px 6px; text-decoration: none; border-radius: 5px; -moz ...
- openssl生成自签名证书
1.生成x509格式的CA自签名证书 openssl req -new -x509 -keyout ca.key -out ca.crt 2.生成服务端的私钥(key文件)及申请证书文件csr文件 o ...