美文网首页
Dapper.net的使用

Dapper.net的使用

作者: czly | 来源:发表于2017-07-01 10:38 被阅读0次

最近项目比较忙,很久没有更新简书了,周末抽个时间,将最近项目中用到的一些框架进行整理。

Dapper.net是一个开源的ORM框架,使用方法非常简单,速度也很快。

使用方法很简单,如下,可以将查询出来的结果转换成List实体,很方便。
<pre>
List<ModelBase> list;
using (var conn = GetSqlConnection())
{
list = conn.Query<ModelBase>("select * from t").ToList();
}
</pre>

在项目的实际使用过程中,我对它进行了一些简单的封装,网上已经有很多的开源DapperNet的扩展,不过我还是自己实现了一个。有Query,UPdate,Add和Execute方法。
<pre>
public class DapperNetExt
{

    private string _SqlConnString;
    /// <summary>  
    /// 获取连接字符串  
    /// </summary>  
    /// <returns></returns>  
    protected IDbConnection GetSqlConnection()
    {

        if (string.IsNullOrEmpty(_SqlConnString))
        {
            _SqlConnString = ConfigurationManager.AppSettings["SqlConn"];
        }

        return new SqlConnection(_SqlConnString);
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="query"></param>
    /// <returns></returns>
    protected List<T> Query<T>(string query)
    {
        List<T> list;
        using (var conn = GetSqlConnection())
        {
            list = conn.Query<T>(query).ToList();
        }
        return list;
    }

    protected List<T> Query<T>(string query, DynamicParameters dp, CommandType commandtype)
    {
        List<T> list;
        using (var conn = GetSqlConnection())
        {
            list = conn.Query<T>(query, dp, null, true, null, commandtype).ToList();
        }
        return list;
    }

    protected DataSet Query(string query)
    {
        DataSet ds = new DataSet();
        using (var conn = GetSqlConnection())
        {
            SqlConnection sqlconn = conn as SqlConnection;
            SqlDataAdapter da = new SqlDataAdapter(query, sqlconn);
            da.Fill(ds);
        }
        return ds;
    }

    protected object ExecuteScalar(string query)
    {
        object obj;
        using (var conn = GetSqlConnection())
        {
            obj = conn.ExecuteScalar(query);
        }
        return obj;
    }

    /// <summary>
    /// 执行
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    protected int Execute(string sql)
    {
        int result = 0;
        using (var conn = GetSqlConnection())
        {
            result = conn.Execute(sql);
        }

        return result;
    }

    protected int Execute(string sql, object obj)
    {
        int result = 0;
        using (var conn = GetSqlConnection())
        {
            result = conn.Execute(sql, obj);
        }

        return result;
    }

    protected int Add<T>(T obj, string keyFiled = null)
    {
        return Add<T>(new List<T>() { obj }, keyFiled);
    }

    protected int Add<T>(List<T> obj, string keyFiled = null)
    {

        Type t = obj.FirstOrDefault().GetType();
        string tableName = t.Name;
        PropertyInfo[] ps = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        string tmpsql = " insert into " + tableName;

        string tmpSqlPara = " ( ";
        string tmpSqlValue = " values ( ";

        foreach (var item in ps)
        {
            if (keyFiled != null)
            {
                if (item.Name == keyFiled)
                {
                    continue;
                }
            }
            tmpSqlPara += item.Name + ",";
            tmpSqlValue += "@" + item.Name + ",";
        }

        tmpSqlPara = tmpSqlPara.Substring(0, tmpSqlPara.Length - 1);
        tmpSqlPara += " ) ";

        tmpSqlValue = tmpSqlValue.Substring(0, tmpSqlValue.Length - 1);
        tmpSqlValue += " ) ";

        //if (keyFiled != null)
        //{
        tmpsql += tmpSqlPara;
        //}
        tmpsql += tmpSqlValue;

        return Execute(tmpsql, obj);
    }

    protected int Update<T>(T obj, string keyFiled)
    {
        return Update<T>(new List<T>() { obj }, keyFiled);
    }

    protected int Update<T>(List<T> obj, string keyFiled)
    {

        Type t = obj.FirstOrDefault().GetType();
        string tableName = t.Name;
        PropertyInfo[] ps = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        string tmpsql = " update " + tableName;

        string tmpSqlPara = " set  ";
        string tmpSqlwhere = " where " + keyFiled + "=@" + keyFiled;

        foreach (var item in ps)
        {
            if (keyFiled != null)
            {
                if (item.Name == keyFiled)
                {
                    continue;
                }
            }
            tmpSqlPara += item.Name + "=@" + item.Name + ",";
        }

        tmpSqlPara = tmpSqlPara.Substring(0, tmpSqlPara.Length - 1);


        tmpsql += tmpSqlPara + tmpSqlwhere;

        return Execute(tmpsql, obj);
    }
}

</pre>

使用的时候,只需写一行代码就可以了。
<pre>
List<ModelBase> list = Query<ModelBase>("select * from t");
</pre>

相关文章

  • Dapper.net的使用

    最近项目比较忙,很久没有更新简书了,周末抽个时间,将最近项目中用到的一些框架进行整理。 Dapper.net是一个...

  • 使用Emit实现给实体赋值

    Dapper.net的速度很快,最近看源码,原来他orm的实现是通过编写大量IL代码实现的。使用DynamicMe...

  • 关于Dapper.NET的相关论述

    年少时,为何不为自己的梦想去拼搏一次呢?纵使头破血流,也不悔有那年少轻狂。感慨很多,最近事情也很多,博客也很少更新...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

网友评论

      本文标题:Dapper.net的使用

      本文链接:https://www.haomeiwen.com/subject/ylpwcxtx.html