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
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.
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.
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));
SQL Server doesn't support array types, but you can pass through a table variable using Table Types.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With