ExecuteScalar编译错误
我在 ExecuteScalar()
上遇到错误。它说我必须声明标量变量@PicID
。我如何声明标量变量?或者我错误地执行了整个功能?
public int GetTotalNoVotes(int PicRating, int PicID)
{
//get database connection string from config file
string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
SqlConnection conn = new SqlConnection(strConectionString);
conn.Open();
SqlCommand oCommand = new SqlCommand("SELECT COUNT(1) AS Expr1 FROM Ratings WHERE PicRating = @PicRating) AND (PicID = @PicID)", conn);
oCommand.Parameters.Add("@PictureID", SqlDbType.Int);
oCommand.Parameters["@PictureID"].Value = PicID;
oCommand.Parameters.Add("@PicRating", SqlDbType.Int);
oCommand.Parameters["@PicRating"].Value = PicRating;
object oValue = oCommand.ExecuteScalar();
conn.Close();
if (oValue == DBNull.Value)
{
return 0;
}
else
{
return Convert.ToInt32(oValue);
}
}
I'm getting an error on ExecuteScalar()
. It says I must declare the scalar variable @PicID
. How could I declare a scalar varaible? Or am I doing this entire function incorrectly?
public int GetTotalNoVotes(int PicRating, int PicID)
{
//get database connection string from config file
string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
SqlConnection conn = new SqlConnection(strConectionString);
conn.Open();
SqlCommand oCommand = new SqlCommand("SELECT COUNT(1) AS Expr1 FROM Ratings WHERE PicRating = @PicRating) AND (PicID = @PicID)", conn);
oCommand.Parameters.Add("@PictureID", SqlDbType.Int);
oCommand.Parameters["@PictureID"].Value = PicID;
oCommand.Parameters.Add("@PicRating", SqlDbType.Int);
oCommand.Parameters["@PicRating"].Value = PicRating;
object oValue = oCommand.ExecuteScalar();
conn.Close();
if (oValue == DBNull.Value)
{
return 0;
}
else
{
return Convert.ToInt32(oValue);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您已经定义了参数 @PictureId 但您在查询中使用了变量名称 @PicId
尝试:
I think you've defined the parameter @PictureId but you've used the variable name @PicId in the query
Try:
这是您的声明代码
,在这种情况下,您必须将 PictureID 更改为 PicID
here is your declare code
and in this case , you must change the PictureID to PicID