user673906
user673906

Reputation: 847

C# SQL Query results to string list

Hi I want to query my database and return all the results as a string At the moment my code is like this

SqlDataReader DR = query.ExecuteReader();
  while (DR.Read())
{
    stringList.Add (DR.GetString(0));
}

With stringList being a list of strings, my question is how do I convert all results to a string. Thanks

Upvotes: 0

Views: 1432

Answers (1)

abhi
abhi

Reputation: 3136

If you wish to join a all the elements of a List to a single string, the way to do it is call the join method on the string class, as D Stanley has suggested.

Here is a simple example.

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

Upvotes: 1

Related Questions