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

YME*_*ELS 6 c# sql-server asp.net

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

提前致谢

Jet*_*hro 15

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());
 }
}
Run Code Online (Sandbox Code Playgroud)

将作者姓名填入List的非常基本的示例.