[Solved] How to convert list to string in C# where the list is gotten from a mssql database? [closed]


In C# if you have the list then you can try this:

string[] str = lst.ToArray();

C# List has ToArray() inbuilt method.

Here is a code with an assumption that you may want to connect to SQL Server from C#, get the select statement rows into a list and then convert that list to a string array: (Fill up the ? marks using proper values as per your system/database. Here is MSDN articles: one and two.

 using (SqlConnection CONN = new SqlConnection("server=?;database=?;Integrated Security=?"))  {
      //e.g. new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=dbname");
 String queryString = "SELECT CustomerID, CompanyName FROM dbo.Customers";
 SqlDataAdapter adapter = New SqlDataAdapter(queryString, CONN);
 DataSet dset = New DataSet(); 
 adapter.Fill(dset, "Customers");

     List<string> lst = new List<string>();
     //iterate through Dataset
     foreach(DataRow row in dset.Tables["Customers"].Rows)
     {
       lst.Add(row["CompanyName"].ToString());
     }
     //to string array
     string[] str = lst.ToArray();
}

2

solved How to convert list to string in C# where the list is gotten from a mssql database? [closed]