返回泛型集合的SqlDBHelper
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Entity; 6 using System.Diagnostics; 7 using System.Data.SqlClient; 8 using System.Data; 9 using System.Configuration; 10 11 namespace DAL 12 { 13 public class SqlDBHelper 14 { 15 private string connectionString; 16 public SqlDBHelper() 17 { 18 connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString; 19 // connectionString = "Server=.;Integrated Security=true;Database=Northwind;uid=sa;pwd=123;"; 20 } 21 [DebuggerNonUserCode] 22 public List<TEntity> ReadAll<TEntity>(string sql,CommandType command, params SqlParameter[] parameters) where TEntity : EntityBase, new() 23 { 24 List<TEntity> entities = new List<TEntity>(); 25 26 using (SqlConnection conn = new SqlConnection(connectionString)) 27 { 28 SqlCommand cmd = new SqlCommand(sql, conn); 29 cmd.CommandType = command; 30 cmd.Parameters.AddRange(parameters); 31 32 conn.Open(); 33 using (SqlDataReader reader = cmd.ExecuteReader()) 34 { 35 while(reader.Read()) 36 { 37 TEntity entity = new TEntity(); 38 entity.ParseSqlDataReader(reader); 39 entities.Add(entity); 40 } 41 } 42 } 43 44 return entities; 45 } 46 } 47 }
转载请注明文章来自:程序猴(http://www.chengxuhou.com/)
posted on 2013-09-10 22:22 程序猴chengxuhou.com 阅读(786) 评论(0) 编辑 收藏 举报