简体   繁体   English

如何将SQL结果从一页传递到另一C#asp.net

[英]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. 我正在构建一个Web应用程序,它将具有多个表单页面,这些页面将用于从数据库中提取数据并导出到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. 为了避免重现要导出到excel的相同代码,我制作了一个输出页面,其中包含一个gridview和一个onclick将我的导出导出到excel代码的按钮。

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. 我通过将数据表存储在会话对象中并将其设置为gridview的数据源,将sql查询或存储过程的结果从表单页传递到输出页。

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 表单页面之一中的C#代码

        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 输出页面上的C#代码

    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. 基本上,您必须将值存储在页面,会话,以sessionid为键的缓存范围之外,对值进行序列化和反序列化的方法很少。

For less accessible page session is fair enough, how about inproc session having this data for frequently accessed page? 对于访问较少的页面会话是足够公平的,对于具有频繁访问页面的数据的inproc会话又如何呢?

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. 简而言之,如果您的页面访问量不高,并且要存储的会话数据适合您的硬件会话,则是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net c#中将GridView的值从一个页面传递到另一个页面? - How to pass value of GridView from one page to another in asp.net c#? 如何在ASP.net C#中将诸如用户名和密码之类的文本框值安全地从一页传递到另一页 - How to pass text box values like username and password securely from one page to another in ASP.net C# 如何在asp.net中将图像从一页传递到另一页 - how to pass image from one page to another in asp.net 如何在asp.net C#中从一帧传递值并在另一帧接收? - How to pass a value from one frame and receive in another frame in asp.net C#? 将 ASP.Net GridView 从一个页面传递到另一个页面 - Pass ASP.Net GridView from one page to another page 无法在asp.net中将参数从一页传递到另一页 - Cant pass parameter from one page to another in asp.net 将一行从网格视图传递到另一个网格视图asp.net C# - Pass one row from grid view to another grid view asp.net c# 从SQL结果ASP.net C#构建表 - Build Table from SQL Results ASP.net C# 如何使用 C# 在 ASP.NET 核心 MVC 中将 model 从一个动作传递到另一个动作而不使用 TempData - How to pass model from one action to another without using TempData in ASP.NET Core MVC using C# 如何在ASP.NET MVC中将所选项目从一页传递到另一页 - How do I Pass Selected items from one page to another page in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM