2014-05-09 42 views
0

我有以下的模板,填入從數據表收到我UL數據:如何在C#中使用,而不是DataTable中的DataSet

DataTable dt = getData(); //Insert your datasource here 

    foreach(DataRow row in dt.Rows){ 
     HtmlGenericControl li = new HtmlGenericControl("li"); 
     li.Attributes.Add("data-trait-id", row["TraitID"].ToString()); 

     HtmlAnchor a = new HtmlAnchor(); 
     a.Attributes.Add("data-trait-id", row["TraitID"].ToString()); 

     HtmlGenericControl span1 = new HtmlGenericControl("span"); 
     span1.Attributes.Add("class", "name"); 
     span1.InnerText = row["Name"].ToString(); 
     a.Controls.Add(span1); 

     HtmlGenericControl span2 = new HtmlGenericControl("span"); 
     span2.Attributes.Add("class", "count"); 
     span2.InnerText = row["Count"].ToString(); 
     a.Controls.Add(span2); 

     li.Controls.Add(a); 
     ulSpecialty_selector.Controls.Add(li); 
    } 

但在我的網頁我使用的數據集從SQL得到列查詢:

protected void Page_Load(object sender, EventArgs e) 
    { 
     using (OleDbConnection connection = new OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;" + "Data Source=svr;Initial Catalog=db;User ID=zh;Password=zha")) { 
      OleDbDataAdapter adapter = new OleDbDataAdapter("SHAPE {SELECT * FROM [db].[dbo].[BookingTable]} ", connection); 

      DataSet dsLocation = new DataSet(); 
      adapter.Fill(dsLocation, "Location"); 
     } 
    } 

如何使用的代碼的第一個塊與代碼的第二塊的工作,所以我可以生成ULLI

我期待仿效以下幾點:

<ul class="ulLocation" id="ulLocation2_selector" runat="server"> 
    <li class="liSubLocation active" data-trait-id="9"> 
     <a href="/locations/new-york/neighborhoods?tags[]=12&amp;tags[]=66" class="premote trait-link large btn" data-trait-id="9"> 
     <span class="check"><i class="icon icon-ok"></i></span> 
     <span class="name">New Rochelle</span> 
     <span class="count">6</span> 
      </a> 
    </li> 
</ul> 
+0

DT = ds.tables [0] –

+0

我假設你知道數據集只是一個數據表的集合,你基本上可以參考的數據表通過將其加載到DataSet中並引用DataSet中的第一個索引表,可以在第一個代碼塊中使用它。像這樣.. DataSet dsLocation = new DataSet(); dsLocation.Tables [0] < - 這是你的數據表,本質上是 –

+0

DataTable dt = dsLocation.Tables [0] –

回答

3

可以

//Assuming oDS is my DataSet 
DataTable oDt = oDS.Tables[0];// If you know name of datatable you may use oDS.Tables["name"] 

DataSet中很容易地轉換數據表到DataSet或反之亦然 的DataTable來自DataSet中的數據表

//Assuming oDT is your DataTable 
DataSet oDs = new DataSet(); 
oDs.Tables.Add(oDT); 
+0

謝謝。我得到它的工作,但我怎樣才能模仿我的問題的李?因爲它似乎與BulledtedList我不能有孩子 – Si8

1

假設您要從GetData()返回數據集,您可以將c焊割如下:

DataSet ds = getData(); 
DataTable dt; 
if (ds.Tables.Count > 0) 
{ 
    dt = ds.Tables[0]; 
} 

foreach(DataRow row in dt.Rows){ 

...

+1

@ Sikni8您可能能夠使用ASPX Repeater控制。從來沒有用過它,因爲它可能適合你。 –

相關問題