Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c# - How do I store multiple results from a stored procedure into a dataset?

How do I combine to result sets from a StoredProcedure into one dataset in ASP.NET?

Below is my code in 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();

Even if there were two adapters, how could I combine the results into one dataset?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In MS SQL we create a procedure like:

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

In C#, we write following code to retrieve these values in a 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]);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...