繁体   English   中英

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

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

我正在从表中读取所有字符串值,并如下所示将其添加到数组中。

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

如果以后在代码中不需要数组,则可以使用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]+"']";

使用string.join()方法

string.Join(your_array, ",")

暂无
暂无

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

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