2010-08-07 68 views
0

我填充數據集兩次,數據集僅保留一個表

表名從未正確設置。 我在數據集中只看到一個表格

什麼是機會?

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

請問你可以發佈你的代碼。也許你的代碼和例子有區別。這個例子中可能只是一個錯誤。另外,您發佈的鏈接對我無效。 – funkymushroom 2010-08-07 16:00:00

+0

在調試器中,嘗試dataset.tables.count和鏈接不起作用。 – SoftwareGeek 2010-08-07 16:02:20

回答

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"]); 
+0

MS提供的示例不正確,我討厭我的MoM – Costa 2010-08-07 16:16:54

+0

@Costa:您使用了哪個示例? (什麼是MoM?) – abatishchev 2010-08-07 16:18:40

+0

看哪!千里眼的力量!我現在應該開始我自己的教會!惡魔OUT! – 2010-08-07 16:33:42