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; } } }
相关文章推荐
- ADO.NET(SqlHelper.cs)
- SqlHelper.cs 使用时要注意的地方
- 做项目时写的数据库操作类。SqlHelper.cs(一)
- sqlhelper.cs
- C#、WinForm、ASP.NET - SQLHelper.cs
- SQLHelper.cs文件代码
- sqlhelper.cs
- 做项目时写的数据库操作类。SqlHelper.cs(二)
- Sqlhelper.cs 下载
- Asp.net三层结构入门示例源码>>SQLserver/SQLHelper.cs
- 防止连接泄漏的两种SQLHelper.cs文件写法
- SqlHelper.cs
- C# SqlHelper.CS
- 利用SqlHelper.cs实现Web程序与数据库的连接
- SQLHelper.cs的经典代码
- 微软SqlHelper.cs数据访问帮助类
- sqlhelper .cs
- C#,ADO封装,SqlHelper.cs
- SqlHelper.cs
- sqlHelper.cs文件样板