gpt4 book ai didi

c# - 如何将存储过程的多个结果存储到数据集中?

转载 作者:太空狗 更新时间:2023-10-29 22:08:36 25 4
gpt4 key购买 nike

如何将 StoredProcedure 的结果集合并到 ASP.NET 中的一个数据集中?

下面是我在asp.net中的代码

SqlDataAdapter adap = new System.Data.SqlClient.SqlDataAdapter("sp_Home_MainBanner_TopStory",con);
adap.SelectCommand.CommandType = CommandType.StoredProcedure;
adap.SelectCommand.Parameters.AddWithValue("@rows", 9);

DataSet DS = new DataSet();

adap.Fill(DS, "Table1");
adap.Fill(DS, "Table2");

GridView1.DataSource = DS.Tables["Table2"];
GridView1.DataBind();

即使有两个适配器,我如何将结果合并到一个数据集中?

最佳答案

MS SQL 中,我们创建如下过程:

[ create proc procedureName
as
begin
select * from student
select * from test
select * from admin
select * from result
end
]

C# 中,我们编写以下代码以在 DataSet 中检索这些值

{
SqlConnection sqlConn = new SqlConnection("data source=(local);initial catalog=bj001;user id=SA;password=bj");
SqlCommand sqlCmd = new SqlCommand("procedureName", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
sda.Fill(ds);
sqlconn.Close();

// Retrieving total stored tables from a common DataSet.
DataTable dt1 = ds.Tables[0];
DataTable dt2 = ds.Tables[1];
DataTable dt3 = ds.Tables[2];
DataTable dt4 = ds.Tables[3];

// To display all rows of a table, we use foreach loop for each DataTable.
foreach (DataRow dr in dt1.Rows)
{
Console.WriteLine("Student Name: "+dr[sName]);
}
}

关于c# - 如何将存储过程的多个结果存储到数据集中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5313935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com