从 C# 中的存储过程返回多个记录集 [英] Return multiple recordsets from stored proc in C#

查看:24
本文介绍了从 C# 中的存储过程返回多个记录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不将 ASP 经典系统转换为 C#

I am having to convert an ASP classic system to C#

我有一个存储过程,最多可以返回 7 个记录集(取决于传入的参数).

I have a stored procedure that can return up to 7 recordsets (depending on the parameters passed in).

我需要知道如何简单地将所有记录集作为单独的数据表返回,以便我可以遍历那里的任何内容,当我到达它的末尾时跳到下一个数据表,而不必运行多个 SQL 语句并使用多个adapter.Fill 语句将每个表添加到数据集中.

I need to know how I can simply return all the recordsets as individual DataTables so that I can loop through whatever is there, skipping to the next DataTable when I get to the end of it without having to run multiple SQL statements and use multiple adapter.Fill statements to add each table into a DataSet.

在经典中,当我到达循环末尾以移动到下一条语句时,它是一个简单的 Do While not objRS.EOF 循环和 objRS.NextRecordset().

In classic it was a simple Do While not objRS.EOF loop with a objRS.NextRecordset() when I got to the end of the loop to move to the next statement.

有什么我可以使用而不需要完全重写当前后端代码的吗?

Is there anything I can use that doesn't require a total rewrite of the current back end code?

每个记录集都有不同的列数和行数.它们彼此无关.我们从存储过程返回多个记录集以减少流量.

Each recordset has a different number of columns and rows. They are unrelated to each other. We return multiple recordsets from Stored Proc's to reduce traffic.

例子会很好.

谢谢

推荐答案

SqlConnection con=new SqlConnection("YourConnection String");
SqlCommand cmd=new SqlCommand();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds = new DataSet();
cmd = new SqlCommand("name of your Stored Procedure", con);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@SuperID", id);//if you have parameters.
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();

在此之后,您可以利用不同的 (7) 记录集使用

After this you can take advantage of different (7) recordsets using

ds.Tables[0]
ds.Tables[1]
ds.Tables[2]
ds.Tables[3]
ds.Tables[4]
ds.Tables[5]
ds.Tables[6]

这篇关于从 C# 中的存储过程返回多个记录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆