C# 怎样把数据库中的多张表一次性的存进同一个DataSet里面

public DataSet DS()
{
string sql = @"select name from sysobjects where xtype='" + "U'";
SqlDataAdapter sda = new SqlDataAdapter(sql,Sc);
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
string tablename = @"select * from " + dr[0];

}
return ds;
}
这样再怎么做?注意是把数据库的多张表存进dataset里面,是多张表存进同一个dataset里面

其实这也很简单,方法如下:
1、动态操作DataSet:简单的说,先给DataSet创建几个table,然后修改sda.Fill(ds);中的ds到其中的具体表,注意这时的sql语句是每次选取一个表;

2、如果想一次性把多个表存储到DataSet中,可以通过存储过程,在存储过程中多写几条select就行了
希望能帮到你 ^-^追问

代码如何写?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-31
string sql = @"select * from student
select * from teacher
select * from score";
SqlDataAdapter sda = new SqlDataAdapter(sql,Sc);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable t1=ds.Tables[0];//student表
DataTable t2=ds.Tables[1];//teacher表
DataTable t3=ds.Tables[2];//score表
return ds;追问

问题是如果100张表,你不能写100个sql语句的

相似回答