使用DataReader的NextResult !

     数据库访问的对象DataReader,包括SqlDataReader,OleDbDataReader等有个NextResult方法,返回一个布尔值,true。鼠标指到NextResult代码上显示这些东东:Advances the data reader to the next result , when reading the results of batch Transact-SQL statement . :)下面是个简单的例子。  
ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Data.SqlClient;
None.gif
namespace UseReader
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection cnn 
= new SqlConnection("data source=(local);initial catalog=tempdb;integrated security=sspi");
InBlock.gif            SqlCommand cmd 
= new SqlCommand("select status,displayname from mytable1;select * from mytable2", cnn);
InBlock.gif            SqlDataReader reader 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cnn.Open();
InBlock.gif                reader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif                
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    listBox1.Items.Add(reader[
"displayname"]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (reader.NextResult())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        listBox2.Items.Add(reader[
"age"]);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(ex.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                reader.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
看了这篇文章你就不用每条select语句占用一个command了。^_^

转载于:https://www.cnblogs.com/netboy/archive/2006/01/11/315524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值