点击率的代码怎么写
新闻页面的思路是这样:在载入页面的时候直接从数据库中取得标题、内容等信息,同时将点击率(newsClick)加1并存入数据库,请问一下应该怎么完成增加点击率的代码。
我看了一下以前的文章,有人说在数据库中写个存储过程,让新闻读取一次点击率加1,这样的存储过程又如何来实现呢?
protected void Page_Load(object sender, EventArgs e)
{
String connStr;
connStr = ConfigurationManager.ConnectionStrings[ "testConnectionString "].ConnectionString;
SqlConnection Conn = new SqlConnection(connStr);
Conn.Open();
String strSQL = "SELECT newsTitle, newsClick, newsContent, newsTime FROM NEWS WHERE newsId = " + Request[ "newsId "];
SqlCommand Conmm = new SqlCommand(strSQL, Conn);
SqlDataReader dr = Conmm.ExecuteReader();
while (dr.Read())
{
String n_title = dr[ "newsTitle "].ToString();
String n_time = dr[ "newsTime "].ToString();
String n_click = dr[ "newsClick "].ToString();
String n_content = dr[ "newsContent "].ToString();
title.Text = (n_title);
time.Text = (n_time);
click.Text = (n_click);
content.Text = (n_content);
}
Conn.Close();
}
------解决方案--------------------最简单的方法应该是在读取完成后在执行一条sql语句,比如
update news set newsclick= newsclick+1 WHERE newsId = " + Request[ "newsId "];
------解决方案--------------------String strSQL = "SELECT newsTitle, newsClick, newsContent, newsTime FROM NEWS WHERE newsId = " + Request[ "newsId "];
改一下,把上面1楼的Update写在前面就行了
String strSQL = "UPDATE........;SELECT ......... ";
后面全部都一样.
另外你的代码写得太危险了,我直接在地址栏里输入UrlEncode过的Sql语句就让你的程序完蛋!