[英]Retrieving data from SQL Server in ASP.NET using C#
I have successfully created a master page to upload and edit data and has successfully retrieved that data at my client page, homepage.我已成功创建了一个母版页来上传和编辑数据,并已在我的客户端页面主页上成功检索到该数据。 Now here is my problem, in my homepage I have 5 repeaters retrieving data from 5 tables both text and image:
现在这是我的问题,在我的主页中,我有 5 个中继器从 5 个文本和图像表中检索数据:
Here is my code for retrieving the data:这是我用于检索数据的代码:
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
private void rpt_slider()
{
try
{
DataTable dt1 = this.GetData("select top(5)* from H_slide");
rpt_sli.DataSource = dt1;
rpt_sli.DataBind();
}
catch (Exception)
{
}
}
And on page load:在页面加载时:
protected void Page_Load(object sender, EventArgs e)
{
// homepage slider
rpt_slider();
// news
rpt_news();
// featured post
rpt_feat();
// principal notes
rpt_notes();
// about college
rpt_about();
}
Retrieving all that data takes a lot of time, especially if the internet speed is slow.检索所有数据需要花费大量时间,尤其是在互联网速度较慢的情况下。 So is it possible to retrieve the data one by one?
那么是否可以一一检索数据呢? First the homepage slider and after a few seconds the news data and after that about the college and so on so that all the data is not loaded at once but at intervals of few seconds.
首先是主页滑块,几秒钟后是新闻数据,然后是关于大学等的数据,因此所有数据不是一次加载,而是每隔几秒钟加载一次。 I have spent days researching but have not found any solution, I don't know if it's possible to do that but if there is any solution will anyone please help me.
我花了几天时间研究,但没有找到任何解决方案,我不知道是否有可能做到这一点,但如果有任何解决方案,有人请帮助我。
Thank you.谢谢你。
I am afraid this is a little more involved than you think.恐怕这比你想象的要复杂一些。 If you need everything on the same page, you will have to get them using AJAX: Get the first set and once the response has returned, then send the subsequent request.
如果您需要同一页面上的所有内容,则必须使用 AJAX 获取它们:获取第一个集合,一旦响应返回,然后发送后续请求。 Here is an article you can follow.
这是您可以关注的文章。 But like I said, it is more involved than you think.
但就像我说的,它比你想象的更复杂。
Also, try and using paging.另外,尝试使用分页。 For example, if one of your tables has 1 million rows, do not get all 1 million.
例如,如果您的一个表有 100 万行,则不要获取所有 100 万行。 Get maybe 50 and have controls so users can press next and then get the next 50 and so on.
可能获得 50 个并拥有控件,以便用户可以按下一步,然后获得下一个 50,依此类推。 Here is a tutorial that you can follow for paging, sorting etc.
这是一个教程,您可以按照它进行分页、排序等操作。
Rather it's really simple, in my humble opinion.相反,在我看来,这真的很简单。 Use task parallel library (this needs .NET 4.5), if the 5 types of data are independent of each other.
使用任务并行库(这需要.NET 4.5),如果5种类型的数据相互独立。 Get all these data asynchronously from db and bind them to repeaters.
从 db 异步获取所有这些数据并将它们绑定到转发器。 This should show you a big performance gain.
这应该会显示出很大的性能提升。
Also follow the suggestions of paging suggested by @codingyoshi.还要遵循@codingyoshi 建议的分页建议。
If this performance improvement isn't enough , come back, I will suggest some other changes.如果这种性能改进还不够,请回来,我会建议一些其他更改。
Parallel.Invoke{()=>(
{
// homepage slider
rpt_slider();
},
{
// news
rpt_news();
},
{
// featured post
rpt_feat();
},
{
// principal notes
rpt_notes();
},
{
// about college
rpt_about();
}
)};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.