1、现有两张表分别为:A、B
2、实体类分别为:
public partial class A { public A(){ } private int _id; private string _Aname; public int id { set { _id = value; } get { return _id; } } public string Aname { set { _Aname = value; } get { return _Aname; } } } public partial class B { public B(){ } private int _id; private string _Bname; public int id { set { _id = value; } get { return _id; } } public string Bname { set { _Bname = value; } get { return _Bname; } } }
3、正常情况下,一张表对应一个实体类,单表查询时如下:
string sql = "SELECT id,Aname FROM A"; SqlDataReader DR = SQLHelp.ExecuteReader(sql, CommandType.Text, null); IList<A> List = new List<A>(); while (DR.Read()) { A sw = new A(); sw.id = DR.GetInt32(0); sw.Aname = DR.GetString(1); List.Add(sw); } DR.Close(); return List;
我的问题是:多表查询时:多表实体类这样设合适吗??????????????????
大家有没有更好的解决办法呢???,要复用性好的。
public partial class A_B { public A_B(){ } private A _Atable = new A(); private B _Btable = new B(); public A Atable { set { _Atable = value; } get { return _Atable; } } public B Btable { set { _Btable = value; } get { return _Btable; } } } string sql = "SELECT A.id,A.Aname,B.id,B.Bname FROM A,B"; SqlDataReader DR = SQLHelp.ExecuteReader(sql, CommandType.Text, null); IList<A_B> List = new List<A_B>(); while (DR.Read()) { A_B sw = new A_B(); sw.A.id = DR.GetInt32(0); sw.A.Aname = DR.GetString(1); sw.B.id = DR.GetInt32(2); sw.B.Bname = DR.GetString(3); List.Add(sw); } DR.Close(); return List;
直接用视图吧,把两个表拼接起来