存储过程返回多个数据集的调用方式

该博客为转载内容,转载自https://www.cnblogs.com/wwhuzhen/archive/2005/11/24/283461.html ,原内容涉及数据库和PHP方面。
以前很少用到,今天突然有一个存储过程返回的是两个数据集,我原来的执行方式如下:
None.gifpublic static DataView RetrunProc(string Proc_Name,SqlParameter[] Para)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
int i;    
InBlock.gif            SqlConnection myCn 
= new SqlConnection(strConn);            
InBlock.gif            SqlDataAdapter MyCmd 
= new SqlDataAdapter(Proc_Name,myCn);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myCn.Open();
InBlock.gif                MyCmd.SelectCommand.CommandType
=CommandType.StoredProcedure;
InBlock.gif                
for(i=0;i<Para.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MyCmd.SelectCommand.Parameters.Add(Para[i]);
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                DataSet Myds 
= new DataSet();        
InBlock.gif            
InBlock.gif                MyCmd.Fill(Myds,
"table");
InBlock.gif                
return Myds.Tables["table"].DefaultView;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Data.SqlClient.SqlException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
throw new Exception(e.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MyCmd.Dispose();
InBlock.gif                myCn.Close();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
这样是没有办法得到第二个数据集的,修改如下
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 调用存储过程并报回DataSet
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Proc_Name"></param>
InBlock.gif        
/// <param name="Para"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif        public static DataSet RunProcForSet(string Proc_Name,SqlParameter[] Para)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
int i;    
InBlock.gif            SqlConnection myCn 
= new SqlConnection(strConn);            
InBlock.gif            SqlDataAdapter MyCmd 
= new SqlDataAdapter(Proc_Name,myCn);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myCn.Open();
InBlock.gif                MyCmd.SelectCommand.CommandType
=CommandType.StoredProcedure;
InBlock.gif                
for(i=0;i<Para.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MyCmd.SelectCommand.Parameters.Add(Para[i]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                DataSet Myds 
= new DataSet();        
InBlock.gif            
InBlock.gif                MyCmd.Fill(Myds);
InBlock.gif                
return Myds;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Data.SqlClient.SqlException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
throw new Exception(e.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MyCmd.Dispose();
InBlock.gif                myCn.Close();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

这样就可以通过MyDs.Tables[0],MyDs.Tables[1]等方式访问返回的多个数据集了.
另外,还有一种通过Adapter的方式,我没有测试.
不过,目前有另外一种需求不知道如何解决,就是在存储过程中的print的内容,程序中有没有办法接收到呢?

转载于:https://www.cnblogs.com/wwhuzhen/archive/2005/11/24/283461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值