• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
悠
閒

博客园    首页       联系   管理    订阅  订阅
獲取多個返回的結果集(關於DataReader對象的NextResult方法的使用)

      
      對於一些查詢語句所返回的多個結果集,可以調用DataReader對象的Nextresult方法來獲得下一個結果集。如果該方法的返回值為false,則表明已經到達最後一個結果集。例如下麵代碼:

public void Page_Load(Object sender,EventArgs e)
{  
    // 连接到本地计算机的student数据库
    String strConn = "server=(local);database=test;Trusted_Connection=yes";
    SqlConnection cn 
= new SqlConnection (strConn);

    // 打开连接
    cn.Open();

    // 创建Command对象,其中包含两个查询语句
    string s1 ="select id from Student;" ;
    string s2 ="select name from Student" ;
    SqlCommand cm 
= new SqlCommand(s1+s2, cn);

    // 创建DataReader对象
    SqlDataReader dr = cm. ExecuteReader();

    // 通过DataReader的Read方法,依次读取一个SQL语句的查询结果
do
{
    
// 通过DataReader的Read方法,依次读取一个SQL语句的查询结果
    while(dr.Read())
    { 
       
object[] cols = new object[2];
        
int n = dr.GetValues(cols);
        Message.Text 
= Message.Text + "   /   " + dr[0].ToString();
    }
    
// 通过DataReader的NextResult()方法,读取下一个SQL语句的查询结果
    Message.Text = Message.Text + " **** ";
  }
while(dr.NextResult());
// 关闭dr
dr.Close();
// 关闭连接
cn.Close();
}

 

顯示結果如下:

      /2001010348/2001011780/2001296531****/张三/李四/王二****

posted on 2009-04-22 09:27  悠閒  阅读(1607)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3