读书人

怎样用存储过程替换数据库直接读写方法

发布时间: 2012-01-09 21:05:42 作者: rapoo

怎样用存储过程替换数据库直接读写方法,实现特定记录的点击数加1。
怎样用存储过程替换数据库直接读写方法,实现特定记录的点击数加1。
这是已经写好的存储过程(调试成功):
ALTER PROCEDURE dbo.AddClickCount @newsid int
AS
update thenews set news_read_count =news_read_count+1 where newsid=@newsid
RETURN

这是我在CS中的代码(也可以正常使用):我该怎样用存储过程替换现有的更新方法呢?
protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
string newsid = Request.QueryString[ "id "];
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings[ "Wlw_SqlDataConnectionString1 "];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;

try
{
String sql = string.Format( "update [thenews] set [news_read_count] =([news_read_count]+1) where newsid={0} ", newsid);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, conn);
conn.Open();
command.ExecuteNonQuery();
}
catch
{
Response.Write( "数据更新失败 ");
}
conn.Close();
}

[解决办法]
sql = "exec AddClickCount " + newsid ;就可
[解决办法]
一样 exec UserLogin "+ username + ", "+password

不过判断的时候不能只判断hasrows.要判断
(int)rader[0]>

读书人网 >asp.net

热点推荐