【发布时间】:2011-08-31 19:09:49
【问题描述】:
我有一个搜索引擎,它将使用网络服务来搜索我的数据库以查找 3 个特定的东西。我什至不知道它是否会像这样工作,但我的主页上有一个下拉列表,可以选择产品、功能、描述。根据用户的选择,Web 服务应该转到 if 语句以使用正确的 SELECT 语句并查找搜索结果。
有人会帮我弄清楚如何修复我所写的内容以使其正常工作吗?请不要太挑剔,我没有太多经验。我也一直在研究 SQL 注入,因为我有很多易受攻击的代码,所以当你查看我的代码时请记住这一点。
我无法让位于 WebService 页面上 DropdownList1.Value 实例下方的蓝色波浪线消失。
网络服务:
<WebMethod()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As String()
Dim Feature As String = DropDownList1.Value
Dim Description As String = DropDownList1.Value
Dim Product As String = DropDownList1.Value
If Feature Then
Dim FeatureSql As String = "Select FeatureTitle FROM Feature WHERE FeatureTitle LIKE " + " " '%" + prefixText + "'"
Dim sqlConn As New SqlConnection("Server=off-db1;uid=productsDB_admin;pwd=******;database=Products")
sqlConn.Open()
Dim myCommand As New SqlCommand(FeatureSql, sqlConn)
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
Dim myTable As New DataTable
myTable.TableName = "FeatureSearch"
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
items.SetValue(dr("FeatureTitle").ToString(), i)
i += 1
Next
Return items
End If
If Description Then
Dim MarketingSql As String = "Select MarketingType, MarketingData FROM Marketing WHERE MarketingType = '2' AND MarketingData LIKE " + " " '%" + prefixText + "'"
Dim sqlConn As New SqlConnection("Server=off-db1;uid=productsDB_admin;pwd=*****;database=Products")
sqlConn.Open()
Dim myCommand As New SqlCommand(MarketingSql, sqlConn)
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
Dim myTable As New DataTable
myTable.TableName = "DescriptionSearch"
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
items.SetValue(dr("MarketingType").ToString(), i)
items.SetValue(dr("MarketingData").ToString(), i)
i += 1
Next
Return items
End If
If Product Then
Dim ProductSql As String = "Select ProductName FROM Product WHERE ProductName LIKE " + " " '%" + prefixText + "'"
Dim sqlConn As New SqlConnection("Server=off-db1;uid=productsDB_admin;pwd=*****;database=Products")
sqlConn.Open()
Dim myCommand As New SqlCommand(ProductSql, sqlConn)
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
items.SetValue(dr("ProductName").ToString(), i)
i += 1
Next
Return items
End If
End Function
End Class
Default.aspx 页面 - 我需要下拉列表以某种方式与数据库绑定。
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
Search by:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Product</asp:ListItem>
<asp:ListItem>Feature</asp:ListItem>
<asp:ListItem>Description</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="Search" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="Search" ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="3" CompletionSetCount="120" EnableCaching="true">
</asp:AutoCompleteExtender>
【问题讨论】:
-
您不应该编写新的 ASMX Web 服务。微软认为它们是“遗留技术”。所有新开发的 Web 服务客户端或服务器都应使用 WCF。
-
哦。那我一定找到了一个很老的教程!开始使用其他东西会复杂多少?
-
一点也不复杂。 WCF 的内容要丰富得多,但您可以完全忽略它。事实上,如果你使用“basicHttpBinding”绑定,你可以让 WCF 看起来很像 ASMX。
-
在 Internet 上查找信息时必须非常小心。您可能会发现旧信息看似全新,但可能会让您走上完全错误的轨道。
-
MVC 是一个 Web 应用程序框架,而不是一个 Web 服务框架。它可以用于 REST-ful Web 服务,但我个人更喜欢使用为 Web 服务制作的框架,而不是为网页制作的框架。
标签: asp.net sql vb.net web-services