Dapper.net的使用

最近項(xiàng)目比較忙,很久沒有更新簡書了,周末抽個時間,將最近項(xiàng)目中用到的一些框架進(jìn)行整理。

Dapper.net是一個開源的ORM框架,使用方法非常簡單,速度也很快。

使用方法很簡單,如下,可以將查詢出來的結(jié)果轉(zhuǎn)換成List實(shí)體,很方便。
<pre>
List<ModelBase> list;
using (var conn = GetSqlConnection())
{
list = conn.Query<ModelBase>("select * from t").ToList();
}
</pre>

在項(xiàng)目的實(shí)際使用過程中,我對它進(jìn)行了一些簡單的封裝,網(wǎng)上已經(jīng)有很多的開源DapperNet的擴(kuò)展,不過我還是自己實(shí)現(xiàn)了一個。有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>
    /// 執(zhí)行
    /// </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>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容