文章访问未明智地显示类别ID [英] article visit not display category id wise
问题描述
在此代码中,它计算所有商品类别的商品访问总和.但是我想显示如果文章类别1有500次访问,而文章类别2有1000次访问,依此类推.我怎样才能做到这一点.请帮助我
in this code it calculates sum of article visit for all article category. But i want to display if article category 1 has 500 visit and article category 2 has 1000 visit so on. How can i do this. Please help me
SqlConnection con = Database.GetConnection();
SqlCommand com = new SqlCommand("SELECT * FROM category WHERE article_allow = 1", con);
int article_visit = 0;
SqlDataReader rs = com.ExecuteReader();
SqlConnection connect = Database.GetConnection();
while (rs.Read())
{
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list WHERE cat_ID = @catID AND article_status=1", connect);
cmd.Parameters.AddWithValue("catID", rs["cat_ID"]);
//Label1.Text=rs["cat_ID"].ToString();
SqlDataReader rst = cmd.ExecuteReader();
while (rst.Read())
{
article_visit = Convert.ToInt32(rst["article_visit"]) + article_visit;
}
rst.Close();
rst.Dispose();
cmd.Dispose();
}
rs.Close();
rs.Dispose();
我已经使用label1只是检查cat_id,它与我的问题无关,我对此发表了评论.现在,如果有人可以告诉我该如何做才能得到结果
I have used label1 to just check the cat_id, its not related to my question i have comment this. now if any one can tell me what i can do for get the result
推荐答案
您的代码放入我的应用程序中并运行它)就可以了.您是否检查过catID值范围?
也许改变
At a cursory glance (i.e. I haven''t cut''n''pasted your code into my app and run it) it looks ok. Have you checked that you get a range of catID values?
Perhaps change
Label1.Text=rs["cat_ID"].ToString();
要
Label1.Text+=rs["cat_ID"].ToString();
进行测试,看看会得到什么?
for testing and see what you get?
不幸的是,我不知道您到底想做什么..;)
但是无论如何,从SQL的角度来看,您应该优化查询,使其不需要2个SQL连接.
尝试执行以下脚本(将两个选择都替换为以下内容):
Hi,
Unfortunately I don''t know exactly what you try to do.. ;)
But anyway, from the SQL point of view, you should optimize your query that you don''t require 2 SQL connections.
Try to execute the following script (replace both selects with the following):
SELECT B.cat_ID as [cat_ID],
A.article_visit AS [Article_Visit]
FROM article_list A
LEFT JOIN category B
ON A.cat_ID = B.cat_ID
WHERE A.article_status = 1
AND B.article_allow = 1
这将返回一个记录集,该记录集在第一行中包含类别ID(名称:[cat_ID]
),在第二行中包含商品访问次数(名称:[Article_Visit]
)(如果我正确理解了您的代码).
请注意,上述SQL脚本未经测试,并且需要根据您的SQL基础结构进行修改. ;)
希望这有助于找到您的问题的解决方案.
最好的问候,
停止
更新:我忘了在脚本中添加where子句.
This will return a recordset containing the category id (name: [cat_ID]
) in the first row and the article-visit count (name: [Article_Visit]
) (if I understand your code correctly) in the second row.
Please be aware of the fact that the above SQL script is untested and coud require modifications according your SQL infrastructure. ;)
Hope this helps in finding a solution to your problem.
Best regards,
Stops
UPDATE: I forgot to add the where clause in the script.
这篇关于文章访问未明智地显示类别ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!