ExecuteScalar编译错误

发布于 2024-12-17 04:26:38 字数 1051 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后彩虹 2024-12-24 04:26:39

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

尝试:

SqlCommand oCommand = new SqlCommand("SELECT COUNT(1) AS Expr1 FROM Ratings WHERE PicRating = @PicRating) AND (PicID = @PictureID)", conn);       

I think you've defined the parameter @PictureId but you've used the variable name @PicId in the query

Try:

SqlCommand oCommand = new SqlCommand("SELECT COUNT(1) AS Expr1 FROM Ratings WHERE PicRating = @PicRating) AND (PicID = @PictureID)", conn);       
笑忘罢 2024-12-24 04:26:39

<块引用>

如何声明缩放变量?

这是您的声明代码

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

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

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

How would I declare a scaler varaible?

here is your declare code

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

and in this case , you must change the PictureID to PicID

oCommand.Parameters.Add("@PicID", SqlDbType.Int);
oCommand.Parameters["@PicID"].Value = PicID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文