我收到异常“必须声明标量变量” @strAccountID”必须声明标量变量
string @straccountid = string.Empty;
sSQL =
"SELECT GUB.BTN,
GUP.CUST_USERNAME,
GUP.EMAIL
FROM GBS_USER_BTN GUB,
GBS_USER_PROFILE GUP
WHERE GUB.CUST_UID = GUP.CUST_UID
AND GUB.BTN = '@straccountID'
ORDER BY
CREATE_DATE DESC"
@straccountid = strAccountID.Substring(0, 10);
代码为针对数据库运行查询
try
{
oCn = new SqlConnection(ConfigurationSettings.AppSettings["GBRegistrationConnStr"].ToString());
oCn.Open();
oCmd = new SqlCommand();
oCmd.Parameters.AddWithValue("@strAccountID", strAccountID);
oCmd.CommandText = sSQL;
oCmd.Connection = oCn;
oCmd.CommandType = CommandType.Text;
oDR = oCmd.ExecuteReader(CommandBehavior.CloseConnection);
我已经声明的变量在我的查询中是否有任何缺陷?
你声明变量的代码在哪里? – 2013-05-09 22:15:48
无论你声明它,SQL都没有看到它 - 你的查询应该有一个'DECLARE @strAccountID varchar(50)'或类似的东西,在某处......它是一个存储过程吗? – 2013-05-09 22:17:56
可能只是一个首选项,但我发现全大写的SQL很难阅读。不是'SELECT gub.Btn,gup.cust_UserName,gup.Email FROM gbs_USER_BTN gub,gbs_USER_PROFILE gup WHERE ...'更容易在眼睛上? – 2013-05-09 22:23:33