简体   繁体   English

我如何从表中读取字符串值,并追加到单个字符串

[英]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'; 如何获取字符串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. 如果以后在代码中不需要数组,则可以使用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());
}

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? 我也不认为您需要第二个代码段中的for循环(通过1?)还是for循环中的1是一个错字?

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

use string.join() method 使用string.join()方法

Example

string.Join(your_array, ",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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