2012-08-29 25 views
1

我正在讀取表中的所有字符串值並將其添加到數組中,如下所示。如何從表中讀取字符串值並將其附加到單個字符串中

da2.Fill(ds, "DNAiusTwitter_Table"); 
int counts = ds.Tables[0].Rows.Count; 
for (int i = 0; i < counts; i++) 
{ 
    names[i, 0] = ds.Tables[0].Rows[i][3].ToString(); 
} 

我怎樣才能獲得字符串append = 'name1','name2','name3','name4';如何傳遞這些值來參考下面的代碼:

for (int i = 0; i < 1; i++) 
{ 
    HtmlGenericControl scriptTagLinks = new HtmlGenericControl("script"); 
    scriptTagLinks.Attributes["type"] = "text/javascript"; 
    string scrip1 = "$(function() {$(\"[src='" + names[i, 0] + "']\"" + ").pinit();});"; 
    var scrip = "var tweetUsers=['" +append + "'," + "'" + splitted[3]+"']"; 
    scriptTagLinks.InnerHtml = scrip; 

    this.Controls.Add(scriptTagLinks); 
} 
+0

只需將它們作爲託管代碼的方法的參數傳遞即可。 – Guillaume

回答

0

使用的string.join()方法

string.Join(your_array, ",") 
+0

我無法使用連接,建議我如何在代碼中使用附加內容。 – user1632134

1

如果你稍後在代碼中不需要數組,我會使用StringBuilder(System.Text命名空間)如果您的表更改大小,則會更好地分配內存。

da2.Fill(ds, "DNAiusTwitter_Table"); 
int counts = ds.Tables[0].Rows.Count; 
StringBuilder appendString = new StringBuilder(); 
for (int i = 0; i < counts; i++) 
{ 
    appendString.AppendFormat("{0},", ds.Tables[0].Rows[i][3].ToString()); 
} 

這將所有數據添加到生成器,然後在第二代碼片段,請執行下列操作的建設者轉換爲字符串剝離在末尾附加逗號。另外我不認爲你需要第二個代碼片段中的for循環(循環遍歷1?)或for循環中的1是一個錯字?

var scrip = "var tweetUsers=['" + appendString.ToString().TrimEnd(new char[] { ',' }) + "'," + "'" + splitted[3]+"']"; 
相關問題