C# 从C中存储的过程返回多个记录集#
我必须将ASP经典系统转换为C# 我有一个存储过程,可以返回多达7个记录集(取决于传入的参数) 我需要知道如何简单地将所有记录集作为单独的数据表返回,这样我就可以遍历其中的任何数据表,在到达下一个数据表的末尾时跳过它,而不必运行多个SQL语句并使用多个adapter.Fill语句将每个表添加到数据集中 在经典中,当我到达循环的末尾移动到下一个语句时,使用objRS.NextRecordset()执行一个简单的Do what not objRS.EOF循环 是否有任何我可以使用的东西不需要完全重写当前的后端代码 每个记录集具有不同数量的列和行。他们彼此无关。我们从存储的进程返回多个记录集以减少通信量 举个例子就好了C# 从C中存储的过程返回多个记录集#,c#,asp.net,sql,datatable,dataset,C#,Asp.net,Sql,Datatable,Dataset,我必须将ASP经典系统转换为C# 我有一个存储过程,可以返回多达7个记录集(取决于传入的参数) 我需要知道如何简单地将所有记录集作为单独的数据表返回,这样我就可以遍历其中的任何数据表,在到达下一个数据表的末尾时跳过它,而不必运行多个SQL语句并使用多个adapter.Fill语句将每个表添加到数据集中 在经典中,当我到达循环的末尾移动到下一个语句时,使用objRS.NextRecordset()执行一个简单的Do what not objRS.EOF循环 是否有任何我可以使用的东西不需要完全重写
谢谢这将归还您所需的一切
using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "yoursp";
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
}
}
如果使用
SqlDataAdapter.fill()
方法填充数据集,则从存储过程返回的每个记录集都将作为数据集中的数据表返回
DataSet dataset = new DataSet();
using (var adapter = new SqlDataAdapter("yourStoredProcedure", yourConnectionString))
{
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.Fill(dataset);
}
for (int i = 0; i < dataset.Tables.Count; i++)
{
// Do something for each recordset
}
您可以使用
if(dr.NextResult())
然后再和里德博士循环
试试这个
string connStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);
try
{
string str1 = "select productid,productname from products;select VendorFName from vendor";
SqlCommand com = new SqlCommand(str1, Con);
com.Connection.Open();
SqlDataReader dr = com.ExecuteReader();
DropDownList1.Items.Add("Select Product Id");
DropDownList2.Items.Add("Select Vendor Name");
while(dr.Read())
{
DropDownList1.Items.Add(dr.GetValue(0).ToString());
}
if (dr.NextResult())
{
while (dr.Read())
{
DropDownList2.Items.Add(dr.GetValue(0).ToString());
}
}
}
catch (Exception ex)
{
}
finally
{
if (Con.State == ConnectionState.Open)
{
Con.Close();
}
}
在此之后,您可以使用
ds.Tables[0]
ds.Tables[1]
ds.Tables[2]
ds.Tables[3]
ds.Tables[4]
ds.Tables[5]
ds.Tables[6]
我从来不知道使用
SqlDataReader
和.ExecuteReader()
和.NextResult()
可以获得多个结果集。谢谢你的详细回答!事实上,我通常更喜欢使用SqlDataReader,因为它只存储当前记录,而不必处理已经读取的内容和尚未读取的内容,从而降低了内存使用率。然而,在比较数据集和数据阅读器时有一个很好的类比。谢谢分享SO链接!我过去使用过SqlDataReader
s,我认为需要维护很多代码。然后我尝试了DataTables
,认为它们更快。在将来编写附加代码时,我必须记住这些备选方案!再次感谢分享!这就是我们所说的完美隔离这就是我用来从存储过程中获取多个记录集的方法。仅为了澄清,记录集存储在ds.Tables
中,您可能需要放置DataSet ds=new DataSet()代码>开头,以便您可以使用(SqlConnection conn=new System.Data.SqlClient.SqlConnection(connString))
块在之外引用它。只是一个想法。是的,你可以把它搬出去。只是举个例子说明如何做;。谢谢,我喜欢这个答案纯粹是因为我已经有了一个数据类,它带有包装函数,可以处理连接,并在类被破坏时处理它们。我只想使用我已有的一个函数并引用数据集中的表。
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();
ds.Tables[0]
ds.Tables[1]
ds.Tables[2]
ds.Tables[3]
ds.Tables[4]
ds.Tables[5]
ds.Tables[6]