Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading SQL Server Column in an Array or List

I want to know how to copy the values contained in a column in sql server database into an Array or a List? I'm using C# in a Web Application Project(ASP.NET)...

Thanks in advance

like image 605
YMELS Avatar asked Jul 04 '11 13:07

YMELS


People also ask

Can we use array in SQL Server?

Define arrays as SQL variables. Use the ARRAY_AGG built-in function in a cursor declaration, to assign the rows of a single-column result table to elements of an array. Use the cursor to retrieve the array into an SQL out parameter. Use an array constructor to initialize an array.

Can we have an array as column in SQL?

An array in structured query language (SQL) can be considered as a data structure or data type that lets us define columns of a data table as multidimensional arrays.

How do I read an array in SQL?

You can retrieve data from an array by using the UNNEST specification to assign array elements to an intermediate result table. For example: -- IDS AND NAMES ARE ARRAYS OF TYPE INTARRAY. INSERT INTO PERSONS(ID, NAME) (SELECT T.I, T.N FROM UNNEST(IDS, NAMES) AS T(I, N));

Is there an array data type in SQL Server?

SQL Server doesn't support array types, but you can pass through a table variable using Table Types.


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

Very basic example to fill author names into List.

like image 74
Jethro Avatar answered Sep 27 '22 18:09

Jethro