您的位置:首页 > 数据库

SQLHelper.cs

2014-08-13 18:50 295 查看
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseUility
{
/// <summary>
/// 数据库使用公共类
/// </summary>
public class SQLHelper
{
public static string Connection
{
get
{
return "Data Source=.;Database=ShortMessage;Integrated Security=true";
}
}

/// <summary>
/// 增删改数据
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static bool Processing(string strSql)
{
using (SqlConnection sqlCon = new SqlConnection(Connection))
{
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand(strSql, sqlCon);
if (sqlCom.ExecuteNonQuery() > 0)
{
return true;
}
return false;
}
}

/// <summary>
/// 指定条件查询,返回实体集合
/// </summary>
/// <returns></returns>
public static List<T> GetTabaleValue<T>(string strSql)
where T : class,new()
{
using (SqlConnection sqlCon = new SqlConnection(DatabaseCommon.Connection))
{
sqlCon.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter(strSql, sqlCon);
DataTable dt = new DataTable();
sqlDA.Fill(dt);
List<T> config = DTToModel<T>(dt);
return config;
}
}

/// <summary>
/// 数据表转换为实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> DTToModel<T>(DataTable dt) where T : class,new()
{
Type type = typeof(T);
PropertyInfo[] files = type.GetProperties();
List<T> list = new List<T>();
foreach (DataRow dr in dt.Rows)
{
T model = new T();
for (int i = 0; i < files.Count(); i++)
{
files[i].SetValue(model, dr[i]);
}
list.Add(model);
}
return list;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: