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'开发者_JAVA百科m using C#
in a Web Application Project(ASP.NET)...
Thanks in advance
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.
See if this helps http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/
First you have to fill records in dataTable and then iterate through all rows of dataTable and add one by one each record to array list. Check this: 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']);
}
精彩评论