在更新时找不到存储过程
本文关键字:存储过程 找不到 更新 | 更新日期: 2023-09-27 18:23:36
我想做的是,当用户点击链接时,点击量会增加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();
您的错误正在发生,因为系统认为您正在尝试查找名为'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
。
您只需要在SqlCommand的构造函数中指定存储的进程名称。
var cmd = new SqlCommand("updateMovieClicks",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@movieTitle", Session["videoName"].ToString() );
cmd.Parameters.AddWithValue("@movieClicks", int.Parse(clicksIncrease));