我想要做的是当用户点击链接时,点击次数会增加1但是我没有做到=(
输出
无法找到存储过程'updateMovieClicks'如何在分享中获胜?','64'
我的存储过程
ALTER PROCEDURE updateMovieClicks
(
@movieTitle varchar(50),
@movieClicks int
)
AS
update MovieListTable set movieClicks=@movieClicks where movieTitle=@movieTitle;
我的程序代码在页面加载
conn.Open();
SqlCommand cmdIncreaseMovieClicks = new SqlCommand("updateMovieClicks'" +
Session["videoName"].ToString() + "','" + clicksIncrease + "'", conn);
SqlParameter movieTitle = new SqlParameter();
SqlParameter movieClicks = new SqlParameter();
cmdIncreaseMovieClicks.CommandType = CommandType.StoredProcedure;
cmdIncreaseMovieClicks.Parameters.Add("@movieClick", SqlDbType.Int).Value
= clicksIncrease+1;
cmdIncreaseMovieClicks.ExecuteNonQuery();
conn.Close();
答案 0 :(得分:2)
您的错误正在发生,因为系统认为您正在尝试查找名为'updateMovieClicks'How to win in share?','64'
的存储过程。这是因为您已将字符串中的参数与存储过程名称连接在一起。
当指定命令类型为StoredProcedure
时,则将参数单独添加到命令文本中。因此,只有存储过程名称包含在命令文本中,并且参数作为单独的对象添加到SqlCommand。
ALTER PROCEDURE updateMovieClicks
(
@movieTitle varchar(50)
)
AS
update MovieListTable set movieClicks=(movieClicks+1) where movieTitle=@movieTitle;
SqlCommand cmdIncreaseMovieClicks = new SqlCommand("updateMovieClicks", conn);
cmdIncreaseMovieClicks.CommandType = CommandType.StoredProcedure;
cmdIncreaseMovieClicks.Parameters.Add("@movieTitle", SqlDbType.nvarchar).Value
= session["videoName"].tostring();
conn.Open();
cmdIncreaseMovieClicks.ExecuteNonQuery();
conn.Close();
这里我还改进了您的存储过程查询,以便在SQL级别计算movieClicks
。
答案 1 :(得分:0)
您只需要在SqlCommand的构造函数中指定存储的proc名称。
var cmd = new SqlCommand("updateMovieClicks",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@movieTitle", Session["videoName"].ToString() );
cmd.Parameters.AddWithValue("@movieClicks", int.Parse(clicksIncrease));