必须声明标量变量 [英] Must Declare Scalar Variable
问题描述
当我试图在我的应用程序来执行以下非查询的SQL命令,我总是得到错误信息 -
When I attempt to execute the following non query sql command in my application I always get the error message -
必须声明标变量'@RateID
Must declare the scalar variable '@RateID'
我有点不知所措,为什么我收到此错误。这里是我的代码:
I'm a bit at a loss as to why I receive this error. Here is my code:
string sqlText = "INSERT INTO tblRates (RateID, IPName, RateName, Rate, DateFrom, DateTo, Active) " +
"VALUES (@RateID, @IPName, @RateName, @Rate, @DateFrom, @DateTo, @Active);";
SqlCommand sqlCom = new SqlCommand(sqlText);
sqlCom.Parameters.Add("@RateID", SqlDbType.Int);
sqlCom.Parameters.Add("@IPName", SqlDbType.Text);
sqlCom.Parameters.Add("@RateName", SqlDbType.Text);
sqlCom.Parameters.Add("@Rate", SqlDbType.Decimal);
sqlCom.Parameters.Add("@DateFrom", SqlDbType.Date);
sqlCom.Parameters.Add("@DateTo", SqlDbType.Date);
sqlCom.Parameters.Add("@Active", SqlDbType.Bit);
this._rateID = NextID();
sqlCom.Parameters["@RateID"].Value = this._rateID;
if (this._ipName == "All")
{
sqlCom.Parameters["@IPName"].Value = DBNull.Value;
}
else
{
sqlCom.Parameters["@IPName"].Value = this._ipName;
}
sqlCom.Parameters["@RateName"].Value = this._rateName;
sqlCom.Parameters["@Rate"].Value = this._rate;
sqlCom.Parameters["@DateFrom"].Value = this._dateFrom;
sqlCom.Parameters["@DateTo"].Value = this._dateTo;
this._active = true;
sqlCom.Parameters["@Active"].Value = this._active;
cSqlQuery cQ = new cSqlQuery(sqlText, "non query");
当我通过一行行脚印,我看到参数 @RateID
被成功添加到的SqlCommand
对象,方法 NextID
正确返回一个整数值,这是接受为值
的 @RateID
参数的,但是当我上执行非查询的SqlCommand
我得到上述错误。
When I step through line by line, I see the parameter @RateID
is successfully added to the sqlCommand
object, the method NextID
correctly returns an integer value which is accepted as the Value
of the @RateID
parameter, but when I execute non query on the SqlCommand
I get the aforementioned error.
任何帮助非常大加赞赏。
Any help much be much appreciated.
推荐答案
您正在设置的 sqlCom
对象 - 和代码看起来不错
You're setting up the sqlCom
object - and the code looks fine.
但后来:看来你只是在执行SQL命令的位置:
But then: it seems you're just executing the SQL command here:
cSqlQuery cQ = new cSqlQuery(sqlText, "non query");
不使用你之前有这样的设置代码!为什么?
not using all that setup code you had before! Why?
参数,所有包含在 sqlCom
对象 - 但你的 cSqlQuery
似乎并没有真正看那个对象 - 只是在SQL语句本身...
The parameters and all are contained in the sqlCom
object - but your cSqlQuery
doesn't seem to actually look at that object - just the SQL statement itself...
如果您使用此行,而不是会发生什么:
What happens if you use this line instead:
sqlCom.ExecuteNonQuery();
请问这项工作没有例外?
Does this work without exception?
这篇关于必须声明标量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!