【发布时间】:2013-05-04 12:48:30
【问题描述】:
我收到异常“必须声明标量变量”@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);
我已经声明了变量。我的查询有问题吗?
【问题讨论】:
-
声明变量的代码在哪里?
-
无论你在哪里声明它,SQL 都看不到它 - 你的查询应该有一个
DECLARE @strAccountID varchar(50)或类似的东西,某处......它是一个存储过程吗? -
可能只是一种偏好,但我发现全大写 SQL 很难阅读。
SELECT gub.Btn, gup.cust_UserName, gup.Email FROM gbs_USER_BTN gub, gbs_USER_PROFILE gup WHERE ...不是更舒服吗? -
@AdamPlocher 我刚刚添加了代码
-
能否包含实际对数据库运行 SQL 语句的代码?
标签: c# sql parameters