Dataset多张表的填充NextResult()方法

从数据库读取多张表不容易啊,终于有点眉目了,写在这里以后用时就方便了

创建个存储过程查询三张表

create proc [dbo].[procTest]
as
select * from bbs_user
select * from [user]
select * from bbs_catalog

如果要去第一个表里面的值就用sqlDataReader对象的Read () 方法就可

                if (reader.Read())
                    {
                        condition.Count=Convert.ToInt32(reader[0]);//读取一条数据
                     }
                    if (reader.NextResult())
                    {
                        DataTable th = new DataTable("thread");//读取一张表放入DataSet对象中
                   th.Load(reader);
                        ds.Tables.Add(th);
                    }

如果想都放入DataSet中那就简单了:连Read()方法都不用了(当让用了就错了,会交替跳过一个表)

                    DataTable th = new DataTable("thread");
                     th.Load(reader);
                     ds.Tables.Add(th);

                    DataTable dt = new DataTable("top");
                    dt.Load(reader);
                    ds.Tables.Add(dt);

                 
                    DataTable dts = new DataTable("user");
                    dts.Load(reader);
                    ds.Tables.Add(dts);

费了老大劲才做出没想到Read()和NextResult()都不用就行。

posted @ 2008-12-15 16:10  华仔2008  阅读(1076)  评论(0编辑  收藏  举报