6

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

2
  • you can retrieve the datas in column to string ,then use split function and then for loop to store it in array Commented Jul 4, 2011 at 13:23
  • How are you planning to use the data in the resulting array? Commented Jul 4, 2011 at 13:42

3 Answers 3

16
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.

Sign up to request clarification or add additional context in comments.

Comments

2

See if this helps http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/

Comments

1

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']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.