简体   繁体   中英

How can i read string values from a table , and append to a single string

I am reading all the string values from a table and adding to an array as follows.

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();
}

How can I get string append = 'name1','name2','name3','name4'; How can I pass those values to this below code:

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);
}

If you don't need the array later in code, I would use a StringBuilder (System.Text namespace), it has better memory allocation if your table changes in size.

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());
}

This will add all the data to the builder, then in the second code snippet, do the following to convert the builder to a string stripping off the additional comma at the end. Also I don't think you need the for loop (loop through 1?) in the second snippet or is the 1 in the for loop a typo?

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

use string.join() method

Example

string.Join(your_array, ",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM