数据集只保留一个表

时间:2010-08-07 15:55:48

标签: asp.net

我填充数据集两次,

表名永远不会正确设置。 我在DataSet中只看到一个表

什么是机会?

public static DataSet GetSchoolTree()
        {
            BLLBase.CreateConnection();
            BLLBase.Connection.Open();

            DataSet dataSet = new DataSet("SS");


            Stages.GetStages(ref dataSet);
            Schools.GetSchools(ref dataSet);

            BLLBase.Connection.Close();

            dataSet.Relations.Add(dataSet.Tables["Schools"].Columns["ID"], dataSet.Tables["dbo.Stages"].Columns["School_ID"]);

            return dataSet;
        }

 internal static void GetSchools(ref DataSet dataSet)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.TableMappings.Add("dbo.Schools", "Schools");

            SqlCommand command = new SqlCommand();
            command.CommandText = "[dbo].[SR_School_ALL]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Connection = BLLBase.Connection;
            adapter.SelectCommand = command;

            adapter.Fill(dataSet);
        }

 internal static void GetStages(ref DataSet dataSet)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.TableMappings.Add("dbo.Stages", "Stages");

            SqlCommand command = new SqlCommand();
            command.CommandText = "[dbo].[Stp_Stages_All]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Connection = BLLBase.Connection;
            adapter.SelectCommand = command;

            adapter.Fill(dataSet);
        }

感谢

1 个答案:

答案 0 :(得分:2)

对数据集调用.Fill()将重新加载它,而不是将新结果加载到不同的表中。而是在数据集中的表上调用.Fill()。您可能想尝试这样的事情:

var ds = new DataSet();
ds.Tables.Add(new DataTable("first"));
adapter1.Fill(ds.Tables["first"]);
ds.Tables.Add(new DataTable("second"));
adapter2.Fill(ds.Tables["second"]);