使用基本的Dapper,好用的轻量级Orm框架
习惯使用了entityframework,linq和lambda表达式的舒适很爽,但是对于那些很想直观的感受到业务代码的童鞋,dapper出现了,而且一直在业界内被称为性能仅次于原生sql,这么好的东西当然要看一看啦。
话不多说,果断上代码
1.先来看一下dapper连接数据库(当然dapper可以支持MS SQL,MYSQL,Mongdb,这些主流的数据库啦)
这里拿MS SQL来连接一下
首先在webconfig里写连接字符串
<add name="SqlServerConnString" connectionString="Data Source=.;Initial Catalog=数据库名称;user=用户名;password=密码;" />
public abstract class DapperConfig {public static string ConnectionString { get { string _connectionString = ConfigurationManager.ConnectionStrings["SqlServerConnString"].ToString(); return _connectionString; } } public SqlConnection OpenConnection() { SqlConnection connection = new SqlConnection(ConnectionString); connection.Open(); return connection; } }
接下来,我们定义一个基础的CURD动作,存储过程也是必须要有的哦
public class DBHelper<T>where T:class {
#region Search /// <summary> /// 查询列表 /// </summary> /// <param name="sql">查询的sql</param> /// <param name="param">替换参数</param> /// <returns></returns> public static List<T> Query(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList(); } } /// <summary> /// 查询第一个数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QueryFirst(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QueryFirst<T>(sql, param); } } /// <summary> /// 查询第一个数据没有返回默认值 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QueryFirstOrDefault(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QueryFirstOrDefault<T>(sql, param); } } /// <summary> /// 查询单条数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QuerySingle(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QuerySingle<T>(sql, param); } } /// <summary> /// 查询单条数据没有返回默认值 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QuerySingleOrDefault(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QuerySingleOrDefault<T>(sql, param); } } #endregion #region Update、Create、Delete /// <summary> /// 增删改 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static int Execute(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Execute(sql, param); } } #endregion #region Storage /// <summary> /// 带参数的存储过程 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static List<T> ExecutePro(string proc, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { List<T> list = con.Query<T>(proc, param, null, true, null, CommandType.StoredProcedure).ToList(); return list; } } #endregion }
好啦,下一步我们进行一个日常的操作
public List<Sku> GetSkuList(string itemNo) { string sql = "select * from Base_Credit where itemno=@ItemNo"; return this.Query<Sku>(sql,new {ItemNo=itemNo}); }
这么一个基本的操作就完成了,是不是特别简单,自己记录一下,不积小流,无以成江河。
自己记录下省的以后到处找。。