您好我想查询我的数据库并将所有结果作为字符串返回 目前我的代码就像这样
SqlDataReader DR = query.ExecuteReader();
while (DR.Read())
{
stringList.Add (DR.GetString(0));
}
stringList是一个字符串列表,我的问题是如何将所有结果转换为字符串。感谢
答案 0 :(得分:1)
如果您希望将List的所有元素连接到单个字符串,那么执行此操作的方法是调用字符串类上的join方法,如D Stanley建议的那样。
这是一个简单的例子。
https://dotnetfiddle.net/96cY4y
public static void Main()
{
List<string> stringList = new List<string>();
stringList.Add("Hello");
stringList.Add("world");
var strOut = string.Join(" ",stringList.ToArray());
Console.WriteLine(strOut);
}