简体   繁体   English

变量名“ @”已被声明

[英]The variable name '@' has already been declared

Part of our solution is a page that displays company-specific information using an ASP Gridview . 解决方案的一部分是使用ASP Gridview显示公司特定信息的页面。 Our method of constructing the SQL that feeds the Gridview is by using C# to build a custom SELECT statement based on a series of user inputs. 我们构造用于馈送Gridview的SQL的方法是使用C#基于一系列用户输入来构建自定义SELECT语句。

Once the user applies their filters through a button click , C# loops through all of their selections ( check boxes and text boxes ) and then propagates those selections to a separate method which constructs a WHERE clause to append to a simple SELECT statement. 一旦用户通过button click应用过滤器,C#就会遍历其所有选择( check boxes and text boxes ),然后将这些选择传播到一个单独的方法,该方法构造WHERE子句以附加到简单的SELECT语句。 We use a Table-Valued Function in the FROM statement , and the only input parameter is from the Querystring and this does not change throughout the process. 我们Table-Valued Function in the FROM statement使用一个Table-Valued Function in the FROM statement唯一的输入参数来自Querystring并且在整个过程中不会改变。

Once the query has been assembled using C#, we apply this query to the SqlDataSource as the Select Command . 使用C#汇编查询后,我们SqlDataSource查询作为Select Command应用于SqlDataSource However, we have recently discovered a very bizarre SQL error that we haven't seen before: 但是,我们最近发现了一个前所未有的非常奇怪的SQL错误:

Errors : 错误:

"The variable name '@' has already been declared . "The variable name '@' has already been declared

Variable names must be unique within a query batch or stored procedure. "

We aren't declaring any variables in our SQL. 我们没有在SQL中声明任何变量。 As stated above, the only input parameter comes from the Querystring , and we access this parameter using both QueryStringParameters in the ASP:SqlDataSource on the ASP side and “ int.Parse(Request.QueryString["id"]).ToString() ” on the C# side while constructing the SQL query. 如上所述,唯一的输入参数来自Querystring ,我们使用ASP端ASP:SqlDataSource中的QueryStringParameters和“ int.Parse(Request.QueryString["id"]).ToString() ”访问此参数在构造SQL查询时,在C#端进行。

After researching this error, I have yet to find an instance where the variable declaration is empty. 研究完此错误之后,我还没有找到变量声明为空的实例。 Most people are getting errors similar to this when they have declared a variable such as '@email' or '@address' twice. 当大多数人两次声明诸如'@email' or '@address'类的变量时,都会收到与此类似的错误。 We have no double declarations, and the fact that the variable in the error is not defined is causing a massive headache. 我们没有双重声明,并且错误中的变量未定义这一事实引起了极大的麻烦。

Has anyone seen anything like this before or have any suggestions on how to further debug? 有没有人看过这样的东西,或者对进一步调试有什么建议?

I'll post some code if need be, but we are mostly interested to see if anyone has seen an error like this before. 如果需要的话,我会发布一些代码,但是我们最感兴趣的是看看以前是否有人看到过这样的错误。

Code : 代码

string MainQueryStr = ResultsPages.SearchString(SearchVariables(), Request,
                ProjectsSqlds, 0, "SELECT DISTINCT dbo.{0}.* FROM dbo.{0}(" + int.Parse(Request.QueryString["id"]).ToString() + ")", 
                "getXyzById", "AbcId");
        StringBuilder SearchQueryStr = new StringBuilder();
        SearchQueryStr.Append(MainQueryStr);
        SearchQueryStr.Append(" ORDER BY AbcName");
        ProjectsSqlds.SelectCommand = SearchQueryStr.ToString();

The search string function is a 500 line method that we can't post right now. 搜索字符串函数是500行的方法,我们现在无法发布。 It is used all over our solution and works as it should. 它已在我们的解决方案中广泛使用,并可以正常工作。 It stitches together strings to create the query. 它将字符串缝合在一起以创建查询。

This is how the SearchString function appends the parameters: 这是SearchString函数附加参数的方式:

 l.Add(ResultsPages.NewSearchQueryString(ABCFiltersTxBx, SearchQueryStringVariableType.String,
            "{1}.AbcID IN (" + ABCFiltersTxBx.Text + ")"));

Where the ABCFiltersTxBx is parsed into a comma separated string. 将ABCFiltersTxBx解析为逗号分隔的字符串。

I should chime in as the supervisor in question here: 我应该在这里担任主管一职:

OK, so we figured out what was happening. 好的,所以我们知道了发生了什么。

What we didn't realize was that the SQLDataSource was taking our appended WHERE clauses and using them as SelectParameters. 我们没有意识到的是,SQLDataSource正在采用附加的WHERE子句并将其用作SelectParameters。 Each parameter we wanted to add to the query that would ultimately feed the SQLDS was then being added as a SelectParameter without us realizing it, and because we hadn't made any explicit parameter declarations, the parameters were added with just "" as the name, leading to the error of "'@' has already been declared". 然后,我们想要添加到查询的最终将最终提供给SQLDS的每个参数都作为SelectParameter添加而没有意识到,并且因为我们没有进行任何显式的参数声明,所以仅以“”作为名称添加了参数,导致错误“'@'已经被声明”。

The most embarrassing part of this whole thing is that our API has already accounted for Parameter Names, but we had unwittingly excluded this part. 整个过程中最令人尴尬的部分是我们的API已经考虑了参数名称,但是我们不知不觉中已经排除了这部分。 Thank you all very much for reading and attempting to help. 非常感谢大家阅读和尝试提供帮助。 We thoroughly appreciate you taking your time to help us brainstorm our solution over here. 非常感谢您抽出宝贵时间来帮助我们在这里进行头脑风暴。

So I suppose the take-home of this whole error is in 2 parts: 因此,我想将整个错误归纳为两个部分:

  1. Know your API. 了解您的API。 When you realize that you screwed it up on your own, graciously thank those that took the time to help you here on StackOverflow (or wherever you seek help), as their time is valuable as well. 当您意识到自己搞砸了时,非常感谢那些花时间在StackOverflow上(或无论您寻求帮助的地方)为您提供帮助的人,因为他们的时间也很宝贵。

  2. "'@' is already declared" would indicate that you have parameters being declared without a name, so when debugging, look through the SQLDS you are using and find any parameters that haven't been explicitly named. “已经声明'@'”表示您已声明参数而没有名称,因此在调试时,请查看正在使用的SQLDS并查找未明确命名的任何参数。

Again, thank you to all who read and offered to help. 再次感谢所有阅读并愿意提供帮助的人。 It's greatly appreciated. 非常感谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 变量名“@Param”已被声明 - The variable name '@Param' has already been declared 错误:变量名“ @Provision”已被声明 - Error: The variable name '@Provision' has already been declared 变量名“ @VarName”已被声明 - The variable name '@VarName' has already been declared Issue SqlCommand 参数错误:“变量名已被声明” - SqlCommand Parameter error : “The variable name has already been declared” 必须声明标量变量/变量已经声明 - Must declare the scalar variable/ Variable has been already declared 如何检查变量是否已经声明? (IsolatedStorage) - How to check if a variable has already been declared or not? (IsolatedStorage) 变量名“ @Order_id”已经声明。 变量名称在查询批处理或存储过程中必须唯一 - The variable name '@Order_id' has already been declared. Variable names must be unique within a query batch or stored procedure '变量名'@'已经声明。 变量名称在查询批处理或存储过程中必须是唯一的。 - 'The variable name '@' has already been declared. Variable names must be unique within a query batch or stored procedure.' 变量名“ @ArrivedDate”已经声明。 变量名称在查询批处理或存储过程中必须唯一 - The variable name '@ArrivedDate' has already been declared. Variable names must be unique within a query batch or stored procedure 变量名“ @sno”已经声明。 变量名称在查询批处理或存储过程中必须唯一 - The variable name '@sno' has already been declared. Variable names must be unique within a query batch or stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM