简体   繁体   中英

How to pass SQL results from one page to another C# asp.net

I am building a web application that will have multiple form pages that will be used to extract data from a database and export to excel.

To save me reproducing the same code to export to excel I have produced an output page containing a gridview and a button that onclick runs my export to excel code.

I pass the results of my sql query or stored procedure from the form page to the output page by storing a datatable in a session object and setting that to be the datasource of the gridview.

However I do not know how big some of the results will be or how many users will be using these forms at any one time, so I anticipate a session object isnt the best idea.

Any thoughts would be greatly appreciated, thanks.

C# code from one of the Form pages

        DataTable myDataTable = new DataTable();

        string myConnectionString = //connectionstring text

        SqlConnection myConnection = new SqlConnection(
            ConfigurationManager.ConnectionStrings[myConnectionString].ConnectionString);

        string mySQL = //built sql query

        SqlDataAdapter myDataAdaptor = new SqlDataAdapter(mySQL,myConnection);

        using(myDataAdaptor)
        {
            myDataAdaptor.Fill(myDataTable);
        }

        Session["Output"] = myDataTable;

        Response.Redirect("output.aspx");

C# code on output page

    protected void Page_Load(object sender, EventArgs e)
    {
        gvOutput.DataSource = Session["Output"];
        gvOutput.DataBind();
    }

You basically have to store your value out of the scope of page, session, cache with sessionid as key, serializing and deserializing the value are few of them.

For less accessible page session is fair enough, how about inproc session having this data for frequently accessed page?

This basically is a design choice which should be taken into consideration depending on traffic on the page, predicted size of data and etc.

Session is precious and can hamper performance if not managed well. Imagine your query returning huge data as your application grows.

To keep it short, if your page is not highly accessed and session data you are storing is appropriate to your hardware session is a fair choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM