2014-02-22 60 views
1

我正在用Visual Studio 2008測試一個asp.net網站。該網站使用了一個ACCESS數據庫。如果用戶僅在搜索字段中輸入一個關鍵字,則以下搜索查詢可以正常工作。對於多個關鍵字,select語句返回空結果。有什麼方法可以重新調整SQL語句,以便可以搜索多個關鍵字?搜索多個關鍵字 - SQL語句 - 訪問數據庫

SelectCommand="SELECT [title] FROM [recipe] WHERE ([title] LIKE '%' + ? + '%')"> 
<SelectParameters> 
<asp:ControlParameter ControlID="searchField" Name="title" 
PropertyName="Text" Type="String" /> 
</SelectParameters> 

在此先感謝。

+0

什麼是你在不同的關鍵字(如逗號或空格)之間使用分離器? –

回答

1

由於MS Access不支持全文搜索,我想我解決了這個問題,這樣就可以搜索使用某表(S)來自單個文本框表單字段的多個關鍵字。這是腳本。迄今爲止效果很好。有什麼辦法讓這個腳本更好嗎?

感謝

<%@ Import Namespace="System.Data.OleDb" %> 

<script runat="server">  
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    If IsPostBack Then 

     Dim dbconn, sql, dbcomm, dbread, searchTxt, arrText, intCount 
     searchTxt = Request.Form("TextBox1") 
     arrText = Split(searchTxt) 
     'Response.Write(search) 

     dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/MyRecipes.mdb")) 
     dbconn.Open() 
     For intCount = 0 To UBound(arrText) 
      sql = "SELECT * FROM recipe WHERE title LIKE '%" + arrText(intCount) + "%' " 
     Next 
     dbcomm = New OleDbCommand(sql, dbconn) 
     dbread = dbcomm.ExecuteReader() 
     customers.DataSource = dbread 
     customers.DataBind() 
     dbread.Close() 
     dbconn.Close() 
    End If 

End Sub 
</script> 

<body> 
<form id="Form1" runat="server"> 
<asp:Repeater id="customers" runat="server"> 

<HeaderTemplate></HeaderTemplate> 

<ItemTemplate> 
<div style="display:block;"> 
<%#Container.DataItem("title")%> 
</div> 
</ItemTemplate> 

<FooterTemplate></FooterTemplate> 

</asp:Repeater> 
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox> 
</form> 

</body> 
0

凱萊,你可以where後更改像下面的例子

[title] Like 1st criteria OR [title] Like 2nd criteria OR [title] Like 3rd criteria; 
+0

謝謝,但上述不起作用在我的情況,因爲只有一個文本輸入搜索字段,並不是很多。此外,用戶可以在搜索字段中輸入的字詞數量不受限制。我正在考慮使用一個循環,將文本字段中的每個單詞都進行搜索,然後搜索該單詞,然後進入第二個單詞。任何想法如何做到這一點? – Gloria

+0

好吧...這聽起來是正確的....我會限制用戶在這種情況下輸入值的空間或逗號,然後你可以拆分它們使用'String.split(',')'或'whitespce '並循環以產生一系列喜歡... –