首页 新闻 会员 周边 捐助

多表查询时,解决实体层调用复用 有更好的解决办法?

0
悬赏园豆:10 [已解决问题] 解决于 2014-01-11 10:42

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;
Ewin的主页 Ewin | 初学一级 | 园豆:17
提问于:2013-02-25 19:04
< >
分享
最佳答案
0

直接用视图吧,把两个表拼接起来

收获园豆:10
晓风拂月 | 菜鸟二级 |园豆:228 | 2013-04-26 17:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册