最近項(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>