-1

我在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);
        }
    }
4

2 回答 2

2

我认为您已经定义了参数 @PictureId 但您在查询中使用了变量名 @PicId

尝试:

SqlCommand oCommand = new SqlCommand("SELECT COUNT(1) AS Expr1 FROM Ratings WHERE PicRating = @PicRating) AND (PicID = @PictureID)", conn);       
于 2011-11-21T00:38:21.750 回答
1

我将如何声明一个缩放器变量?

这是您的声明代码

oCommand.Parameters.Add("@PictureID", SqlDbType.Int);
oCommand.Parameters["@PictureID"].Value = PicID;

在这种情况下,您必须将 PictureID 更改为 PicID

oCommand.Parameters.Add("@PicID", SqlDbType.Int);
oCommand.Parameters["@PicID"].Value = PicID;
于 2011-11-21T00:44:24.000 回答