正在读取数组或列表中的SQL Server列

本文关键字:SQL Server 列表 读取 数组 | 更新日期: 2023-09-27 18:01:11

我想知道如何将sql server数据库中一列中包含的值复制到ArrayList中?我正在Web应用程序项目(ASP.NET(中使用C#

提前感谢

正在读取数组或列表中的SQL Server列

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

将作者姓名填入列表的非常基本的示例。

看看这是否有帮助http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/

首先必须填充dataTable中的记录,然后遍历dataTable的所有行,并将每个记录逐个添加到数组列表中。检查此项: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']);
}