A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许超 黑马帝   /  2011-12-29 16:42  /  1688 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

DateSet中怎么多表拼接查询

评分

参与人数 1技术分 +2 收起 理由
李荣壮 + 2

查看全部评分

1 个回复

倒序浏览
本帖最后由 傅涌钦 于 2011-12-31 04:34 编辑

无奈!天都快亮了!!举两个表的,再多的应该是一个模式了:



App.config:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <connectionStrings >
  4.     <add name="connstr" connectionString="Server=(local);Integrated Security=SSPI;uid=user id;pwd=password;Database=mydb"/>
  5.   </connectionStrings>

  6. </configuration>
复制代码
C# Code;
  1.         static void Main(string[] args)
  2.         {

  3.             //ExcuteDataReader()多表查询
  4.             //String connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

  5.             //using (SqlConnection conn = new SqlConnection(connstr))
  6.             //{

  7.             //    conn.Open();
  8.             //    using (SqlCommand cmd = conn.CreateCommand())
  9.             //    {
  10.             //        cmd.CommandText = "select Student.Sno,Sname,Cno,cj from Student,SC where Student.Sno=SC.Sno";
  11.             //        SqlDataReader reader = cmd.ExecuteReader();
  12.             //        while (reader.Read())
  13.             //        {
  14.             //            Console.WriteLine("学号:{0} \t 名字:{1}\t 课程号:{2}\t 成绩:{3}\t ", reader[0], reader[1], reader[2], reader[3]);
  15.             //        }
  16.             //    }
  17.             //}

  18.             //DataSet多表查询


  19.             String connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

  20.             using (SqlConnection conn = new SqlConnection(connstr))
  21.             {

  22.                 conn.Open();
  23.                 using (SqlCommand cmd = conn.CreateCommand())
  24.                 {
  25.                     //cmd.CommandText = "select Student.Sno,Sname,Cno,cj from Student,SC where Student.Sno=SC.Sno";

  26.                 }
  27.                 DataSet ds = new DataSet();


  28.                 //填充数据
  29.                 SqlDataAdapter adatapter = new SqlDataAdapter("select * from SC", conn);
  30.                 adatapter.Fill(ds, "SC");
  31.                 SqlDataAdapter adatapter1 = new SqlDataAdapter("select * from Student", conn);
  32.                 adatapter1.Fill(ds, "Student");






  33.                 //创建两表关联
  34.                 ds.Relations.Add("SC", ds.Tables["Student"].Columns["Sno"], ds.Tables["SC"].Columns["Sno"]);



  35.                 //迭代Student表;

  36.                 foreach (DataRow student in ds.Tables["Student"].Rows)
  37.                 {



  38.                     Console.WriteLine("学号:{0}\t 姓名:{1}\t\n", student["Sno"], student["Sname"]);

  39.                     //遍历选课表

  40.                     foreach (DataRow sc in ds.Tables["SC"].Rows)
  41.                     {

  42.                         Console.WriteLine("课程号:{0}\t 成绩:{1}\t", sc["Cno"], sc["cj"]);

  43.                     }

  44.                 }

  45.             }

  46.             Console.ReadKey();
  47.         }
复制代码
:传说用LINQ 会比较简单,还没学到,不了解,伤感!

评分

参与人数 1技术分 +2 收起 理由
李荣壮 + 2

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马