有一个用户表,表里的用户会有几项内容分别在另外几个不同的表中,这几个表中每个表中有某一用户的几条记录(数量不等)。
现在想把这几个表里关于这个用户的所有信息同时显示如何操作?如果分别操作需要多次读取数据库,有没有简洁的办法用视图和过程一次性读取排列?
三个表如下:表名:usertable '用户表,记录用户信息,比如用户A
[userid]
[username]
[userpwd]
[userinfo]表名:tableA '这个表中记录用户所参加活动的id (比如A用户有3条记录)
[id]
[userid]
[gameid]表名:tableB '这个表中记录用户所看的新闻id (比如A用户有5条记录)
[id]
[userid]
[newsid]表名:tableC '这个表中记录用户订阅的服务类型id (比如A用户有2条记录)
[id]
[userid]
[serviceid]目的就是要把用户A所参加的活动记录用表列出来,把A所看的新闻记录用表列出来,再把
A订阅的服务类型用表列出来。显示出来是三张表,有没有简洁的办法,不用分三次读取数据?

解决方案 »

  1.   

    把字段名变一下用union all联合查询就行了。
      

  2.   

    拼凑sql语句:
    sqlConn = new SqlConnection(dbConnStr);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = sqlConn;
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    sqlConn.Open();
    sql="select * from usertable;select * from tableA ;select * from tableB;select * from tableC";
    cmd.CommandText = sql;
    sda.Fill(ds);//显示第一个表
    DataGrid1.DataSoruce = ds.Tables[0].DefaultView;
    DataGrid1.DataBind();
    //显示第二个表
    DataGrid2.DataSoruce = ds.Tables[1].DefaultView;
    DataGrid2.DataBind();
    //显示第三个表
    DataGrid3.DataSoruce = ds.Tables[2].DefaultView;
    DataGrid3.DataBind();
    //显示第四个表
    DataGrid4.DataSoruce = ds.Tables[3].DefaultView;
    DataGrid4.DataBind();
      

  3.   

    cndsn(磐石) 
    你好,本贴我发在了三个位置,另外两个在:
    http://community.csdn.net/Expert/topic/4067/4067078.xml?temp=.9385187
    http://community.csdn.net/Expert/topic/4067/4067077.xml?temp=.3861963基本上回答中只有你的显示结果符合要求,可能也只是你看懂了我的意图,谢谢
    如果没有更好的办法出来,请你到以上的另外两个贴子中接分,谢谢