我知道这个主题到处都是,但我没有INSERT
,UPDATE
,DELETE
。我的陈述是一个简单明了的SELECT
陈述,到目前为止,我已经在我的数据库中处理了116个不同的项目,直到我找到它。
我有一个搜索引擎,我正在浏览数据库中的每个产品,以便向其中添加信息。这一切都是通过网站完成的,但是当我搜索ProductID 331并点击它时,它会转到显示Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
的错误页面
对我来说没有任何意义,只有这一个产品的网站会出错。 这是我正在使用的声明。有谁知道为什么1个产品会导致这个错误?
WebService的:
Public Class ProductSearch
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetProducts(ByVal prefixText As String, ByVal count As Integer)
As String()
Dim ProductSql As String = "Select DISTINCT ProductID, ProductName
FROM Product WHERE ProductName
LIKE '%' & @prefixText & '%'
ORDER BY ProductName ASC"
Using sqlConn As New SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
("LocalSqlServer").ConnectionString)
sqlConn.Open()
Dim myCommand As New SqlCommand(ProductSql, sqlConn)
myCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50)
.Value = prefixText
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
Dim myTable As New DataTable
myTable.TableName = "ProductSearch"
myTable.Load(myReader)
sqlConn.Close()
Dim items As String() = New String(myTable.Rows.Count - 1) {}
Dim i As Integer = 0
For Each dr As DataRow In myTable.Rows
Dim id As String = dr("ProductID").ToString()
Dim name As String = dr("ProductName").ToString()
Dim item As String = AjaxControlToolkit.AutoCompleteExtender
.CreateAutoCompleteItem(name, id)
items.SetValue(item, i)
i += 1
Next
Return items
End Using
End Function
End Class
调用webservice的aspx页面:
<%@ Page Title="Product Search" Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
function AutoCompleteClientMethod(source, eventArgs) {
var value = eventArgs.get_value();
window.location = ("/Product/Default.aspx?id=" + value)
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ProductSearch.asmx" />
</Services>
</asp:ScriptManager>
<asp:TextBox ID="Search" runat="server" AutoComplete="off"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="Search" ServicePath="~/ProductSearch.asmx"
ServiceMethod="GetProducts" MinimumPrefixLength="1" CompletionSetCount="120"
EnableCaching="true" OnClientItemSelected="AutoCompleteClientMethod">
</asp:AutoCompleteExtender>
</div><!--End of main div -->
</asp:Content>
更新:11/9/2011 - 我发现了更多有此问题的记录。它们是ProductID 331-335。我不知道这里发生了什么。可能是那些产品确实不存在或者它们有某种错误?
以下是产生此错误的ProductID及其相应ProductNames的列表:
122 'Managed account section of the Web Site'
331 'Elliott Wave Principle Key to Market Behavior'
332 'Targeting Profitable Entry & Exit Points'
333 'Essentials of Trading It's not WHAT You Think, It's HOW You Think'
334 'Exceptional Trading The Mind Game'
335 'Fibonacci Analysis'
答案 0 :(得分:3)
我认为这是子选择查询,DISTINCT并不意味着一个结果。您可以使用TOP 1来保证一个结果,但不保证它是您想要的结果。
Select TOP 1 DISTINCT ProductID, ProductName
FROM Product WHERE ProductName
LIKE '%" & prefixText & "%'
ORDER BY ProductName ASC
答案 1 :(得分:3)
除了Rick的回答,我还要补充一点,你永远不应该连接字符串来形成SQL语句。请改用参数化查询。字符串连接会使您暴露于SQL注入攻击。此外,通过使用参数化查询,如果可以重用查询计划,则可以获得性能。
See this other StackOverflow post for a good discussion regarding parametrized queries on VB.NET.
答案 2 :(得分:0)
我弄清楚问题是什么。出于某种原因,这些有问题的产品在数据字段中分配了多个值,应该只有一个项目。数据库最近已经更改,所以不会发生,但我想这5个产品已经搞砸了,现在已经被发现了。
感谢所有帮助人员!我希望我能早点考虑进一步检查数据库。 (大约有15个表,所以通常我认为最后要做的)