Reading a SQL Server column in an array or list

I want to know how to copy the values ​​contained in a sql server database column to an Array or List ? I am using C# in a web application project (ASP.NET) ...

Thanks in advance

+6
source share
3 answers
 using (SqlConnection cnn = new SqlConnection("server=(local);database=pubs;Integrated Security=SSPI")) { SqlDataAdapter da = new SqlDataAdapter("select name from authors", cnn); DataSet ds = new DataSet(); da.Fill(ds, "authors"); List<string> authorNames = new List<string>(); foreach(DataRow row in ds.Tables["authors"].Rows) { authorNames.Add(row["name"].ToString()); } } 

A very simple example to populate the names of authors in a List.

+14
source

First you must fill in the entries in the dataTable, and then iterate over all the rows of the dataTable and add one each entry to the list of arrays. Check this out: http://www.dreamincode.net/code/snippet1864.htm

 ArrayList obj = new ArrayList(); for(int x= 0;x<dtGet.Rows.Count;x++) { obj.Add(dtGet.Rows[x]['col_name']); } 
+1
source

Source: https://habr.com/ru/post/891963/


All Articles