数据访问层(实体好么?)
到底是用实体类还是用DataSet呢让人烦恼啊.
DataSet取出的数据容易出错~~,报空引用,容易在取出数据的时候输入错误的列名,比如不注意大小写啦~~我经常犯错,不能以面向对象的形势访问DataSet.
第一:一种是用实体类
public class Info
{
private int _Id;
public Info() {}
public Info(int id)
{
this.Id = id;
}
public int Id
{
get { return Id; }
set { Id = value; }
}
}
public static List<Info> GetAllInfo()
{
SqlConnection connection = new SqlConnection("");
SqlCommand CMD =new SqlCommand("select *from Info", connection);
SqlDataReader dr = null;
List<Info> AllIF=new List<Info>();
try
{
connection.Open();
dr = CMD.ExecuteReader(CommandBehavior.SingleResult);
while (dr.Read())
{
AllIF.Add(new Info(Convert.ToInt32(drdr["UserId"]==null?"0",drdr["UserId"].ToString()));
}
}
finally
{
connection.Close();
}
return AllIF;
}
当然这只是个演示我觉得不太好