加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 读取数组或列表中的SQL Server列

发布时间:2020-12-15 18:02:30 所属栏目:百科 来源:网络整理
导读:我想知道如何将sql server数据库中的列中包含的值复制到数组或列表中? 我在Web应用程序项目(ASP.NET)中使用C#… 提前致谢 解决方法 using (SqlConnection cnn = new SqlConnection("server=(local);database=pubs;Integrated Security=SSPI")) { SqlDataAda
我想知道如何将sql server数据库中的列中包含的值复制到数组或列表中?
我在Web应用程序项目(ASP.NET)中使用C#…

提前致谢

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读