ASPX頁面代碼如下:
<td class="record" height="20">關 鍵 詞:
<asp:textbox id="txtQuery" accessKey="Q" runat="server" Width="250px" MaxLength="50"></asp:textbox>
<asp:button id="btnSearch" runat="server" Text="搜索" EnableViewState="False"></asp:button></td>
</tr>
<tr>
<td height="10">查詢方式:
<asp:dropdownlist id="cboQueryType" accessKey="T" runat="server" Width="250px" EnableViewState="False">
<asp:ListItem Value="All" Selected="True">包含全部的字詞</asp:ListItem>
<asp:ListItem Value="Any">包含任何一個字詞</asp:ListItem>
<asp:ListItem Value="Boolean">布爾表達式查詢</asp:ListItem>
<asp:ListItem Value="Exact">全字匹配</asp:ListItem>
<asp:ListItem Value="Natural">自然語言查詢</asp:ListItem>
</asp:dropdownlist>
</td>
</tr>
<tr>
<td class="record" height="20">查詢範圍:
<asp:dropdownlist id="cboDirectory" accessKey="D" runat="server" Width="250px" EnableViewState="False">
<asp:ListItem Value="/" Selected="True">整個網站</asp:ListItem>
</asp:dropdownlist>
</td>
</tr>
<tr>
<td class="record" height="20"><asp:label id="lblResultCount" runat="server" Font-Italic="True" visible="False" EnableViewState="False"></asp:label></td>
</tr>
<tr>
<td vAlign="top" height="210">
<asp:datagrid id="dgResultsGrid" runat="server" PageSize="15" AllowPaging="True" AutoGenerateColumns="False"
Visible="False" GridLines="None" EnableViewState="False">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="排名">
<HeaderStyle Width="40px"></HeaderStyle>
<ItemTemplate>
<%# (cint(DataBinder.Eval(Container, "DataSetIndex"))) + 1 %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="文檔信息">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<p>
<a href='<%# DataBinder.Eval(Container.DataItem, "VPath")%>' target="_blank">
<%# GetTitle(Container.DataItem)%>
</a>
<br>
<%# GetCharacterization(Container.DataItem)%>
<br>
<i><a href='<%# DataBinder.Eval(Container.DataItem, "VPath")%>' target="_blank">http://<%# Request.ServerVariables("SERVER_NAME")%><%# DataBinder.Eval(Container.DataItem, "VPath")%></a>
-
<%# GetFileSize(Container.DataItem)%>
k </i>
</p>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid>
</td>
</tr>
後臺代碼,使用VB.NET書寫:
Private Sub BindSearch()
Dim dbAdapter As OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
Dim oleDbSelectCommand1 As OleDbCommand = New System.Data.OleDb.OleDbCommand
Dim dbConnection As OleDbConnection = New System.Data.OleDb.OleDbConnection
Dim ds As DataSet = New DataSet("Results")
Dim rows As Integer = 0
dbAdapter.SelectCommand = oleDbSelectCommand1
oleDbSelectCommand1.Connection = dbConnection
dbConnection.ConnectionString = "Provider=MSIDXS.1;Integrated Security .='';Data Source=Web"
Try
dbAdapter.SelectCommand.CommandText = Command
If CStr(ViewState("KEY")) <> "" Then
dbAdapter.Fill(ds)
rows = ds.Tables(0).Rows.Count
Else
lblResultCount.Text = "請輸入關鍵詞進行查詢!"
End If
If Not ds Is Nothing AndAlso rows > 0 Then
' 自定義分頁,與查詢邏輯無關
ViewState("TOTALROWS") = rows
CalculatePage()
dgResultsGrid.AllowPaging = True
dgResultsGrid.PageSize = CInt(ViewState("PAGESIZE"))
dgResultsGrid.CurrentPageIndex = CInt(ViewState("PAGEINDEX")) - 1
lblResultCount.ForeColor = Color.Black
lblResultCount.Text = String.Format("找到 {0} 個相關網頁", rows)
dgResultsGrid.DataSource = ds
dgResultsGrid.DataBind()
dgResultsGrid.Visible = (rows > 0)
Else
ViewState("TOTALROWS") = 0
CalculatePage()
End If
Catch ex As Exception
lblResultCount.ForeColor = Color.Red
lblResultCount.Text = String.Format("無法執行特定的查詢: {0}", ex.Message)
dgResultsGrid.Visible = False
Finally
lblResultCount.Visible = True
End Try
' 自定義分頁
BindNavigate()
End Sub
Get
Dim query As String
query = String.Format("SELECT Rank, VPath, DocTitle, Filename, Size, Characterization, Write FROM SCOPE('DEEP TRAVERSAL OF ""{0}""') WHERE Not CONTAINS(FileName,'""*.txt"" OR ""*.js"" OR ""*.css"" OR ""*.config"" OR ""*.xml""')", CStr(ViewState("DIR")))
Dim type As String = CStr(ViewState("TYPE")).ToLower()
Dim fmt As String = " AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))"
Dim text As String = CStr(ViewState("KEY")).Replace(";", "").Trim
If type = "all" Or type = "any" Or type = "boolean" Then
Dim words() As String = Split(text, " ")
Dim len As Integer = words.Length
Dim i As Integer
For i = 0 To len - 1 Step i + 1
Dim word As String = words(i)
If type = "boolean" AndAlso (String.Compare(word, "and", True) = 0 OrElse String.Compare(word, "or", True) = 0 OrElse String.Compare(word, "not", True) = 0 OrElse String.Compare(word, "near", True) = 0) Then
ElseIf word <> "" Then
words(i) = String.Format("""{0}""", word)
If i < len - 1 Then
If type = "all" Then
words(i) += " AND"
ElseIf type = "any" Then
words(i) += " OR"
End If
End If
End If
Next
query += String.Format(fmt, String.Join(" ", words))
ElseIf type = "exact" Then
query += String.Format(fmt, text)
ElseIf type = "natural" Then
query += String.Format(" AND FREETEXT('{0}')", text)
End If
query += " ORDER BY Rank DESC"
Return query
End Get
End Property
Protected Function GetTitle(ByVal value As Object) As Object
Dim title As String = Convert.ToString(DataBinder.Eval(value, "DocTitle"))
If Not (title Is Nothing) And title.Length > 0 Then
Return title '
Else
Return DataBinder.Eval(value, "Filename")
End If
End Function 'GetTitle
' 取摘要
Protected Function GetCharacterization(ByVal value As Object) As String
Return Server.HtmlEncode(Convert.ToString(DataBinder.Eval(value, "Characterization")))
End Function
' 取文件尺寸,單位KB
Protected Function GetFileSize(ByVal value As Object) As String
Return Convert.ToString(CInt(Convert.ToInt32(DataBinder.Eval(value, "Size")) / 1000))
End Function