栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用SqlCommand返回多个结果集?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用SqlCommand返回多个结果集?

请参见SqlDataReader.NextResult(通过调用SqlCommand.ExecuteReader返回SqlDataReader ):

在读取批处理Transact-SQL语句的结果时,将数据读取器前进到下一个结果[set]。

例:

string commandText = @"SELECt Id, ContactIdFROM dbo.Subscriptions;SELECt Id, [Name]FROM dbo.Contacts;";List<Subscription> subscriptions = new List<Subscription>();List<Contact> contacts = new List<Contact>();using (SqlConnection dbConnection = new SqlConnection(@"Data Source=server;Database=database;Integrated Security=true;")){    dbConnection.Open();    using (SqlCommand dbCommand = dbConnection.CreateCommand())    {        dbCommand.CommandText = commandText;        using(SqlDataReader reader = dbCommand.ExecuteReader())        { while(reader.Read()) {     subscriptions.Add(new Subscription()     {         Id = (int)reader["Id"],         ContactId = (int)reader["ContactId"]     }); } // this advances to the next resultset  reader.NextResult(); while(reader.Read()) {     contacts.Add(new Contact()     {         Id = (int)reader["Id"],         Name = (string)reader["Name"]     }); }        }    }}

其他例子:

  • C#多个结果集
  • 使用SqlDataReader执行返回多个结果集的查询:SqlCommand选择«ADO.Net«C#/ CSharp教程


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/575952.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号