最近項目比較忙,很久沒有更新簡書了瑰钮,周末抽個時間冒滩,將最近項目中用到的一些框架進行整理。
Dapper.net是一個開源的ORM框架浪谴,使用方法非常簡單开睡,速度也很快。
使用方法很簡單苟耻,如下篇恒,可以將查詢出來的結果轉換成List實體,很方便凶杖。
<pre>
List<ModelBase> list;
using (var conn = GetSqlConnection())
{
list = conn.Query<ModelBase>("select * from t").ToList();
}
</pre>
在項目的實際使用過程中胁艰,我對它進行了一些簡單的封裝,網上已經有很多的開源DapperNet的擴展智蝠,不過我還是自己實現(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>