您的位置:首页 > 移动开发

How to use an ASP.NET application to query an Indexing Service catalog by using Visual Basic .NET

2011-07-21 16:43 891 查看
This step-by-step article describes how to create an Indexing Service catalog. It also describes how to use an ASP.NET Web application to query Indexing Service catalogs.

Back to the top

Create a test folder

Create a new folder on the C drive of your computer. Name the folder myCatalogFolder.
Start Notepad, and then paste the following text in a blank document: This is test document to test the index server query and this file name is IndexText.text.
Save the file as C:\myCatalogFolder\IndexText.txt.
Back to the top

Create an Indexing Service catalog

Click Start, and then click Control Panel.
Double-click Administrative Tools.
Click Computer management.
Expand Services and Applications.
Right-click Indexing Service, point to New, and then click Catalog.
In the Name text box, type TestCatalog.
Click Browse, locate the folder where you want to put the catalog, and then click OK two times.
Following message appears under New Catalog Created: Catalog will remain off-line until Indexing Service is restartedClick OK.
Right-click Indexing Service, and then click Stop to stop the Indexing Service.
Right-click Indexing Service, and then click Start to restart the Indexing Service.
Back to the top

Define the scope of the catalog

After you create a new catalog, add the folders that you want included in the scope of the catalog. The scope is the set of folders that is included in and excluded from the catalog. The scope defines the content that is included and excluded from the index. For each folder that is included or excluded, all its subfolders are also included or excluded.
Double-click TestCatalog (the catalog that you created in the "Create a Test Folder" section).
Right-click Directories, point to New, and then click Directory.
Click Browse, locate and then click the folder that you want to add (C:\myCatalogFolder\), and then click OK.

Note Under Include in Index ?, click Yes.
Back to the top

Create an ASP.NET Web application

Start Microsoft Visual Studio .NET.
Use Visual Basic .NET to create a new ASP.NET Web Application project. Name the project IndexQueryApp. By default, WebForm1.aspx is created.
In Design view, right-click WebForm1, and then click View HTML Source.
Replace the existing HTML code with following sample code.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="IndexQueryApp.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Query an Indexing Service Catalog</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 238px; POSITION: absolute; TOP: 76px" runat="server"></asp:TextBox>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 153px; POSITION: absolute; TOP: 127px"
runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="DocTitle" HeaderText="DocTitle"></asp:BoundColumn>
<asp:BoundColumn DataField="filename" HeaderText="FileName"></asp:BoundColumn>
<asp:BoundColumn DataField="size" HeaderText="Size"></asp:BoundColumn>
<asp:BoundColumn DataField="path" HeaderText="PATH"></asp:BoundColumn>
<asp:BoundColumn DataField="url" HeaderText="URL "></asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 638px; POSITION: absolute; TOP: 145px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>

In Solution Explorer, right-click WebForm1, and then click View Code. Replace the existing code with the following sample code.
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

'Web Form Designer requires this call.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: Web Form Designer requires this method call.
'Do not modify it by using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strCatalog As String

' Catalog Name
strCatalog = "TestCatalog"

Dim strQuery As String
strQuery = "Select DocTitle,Filename,Size,PATH,URL from SCOPE() where FREETEXT('" & TextBox1.Text & "')"
' TextBox1.Text is word that you type in the text box to query by using Index Service.
'
Dim connString As String = "Provider=MSIDXS.1;Integrated Security .='';Data Source='" & strCatalog & "'"

Dim cn As New System.Data.OleDb.OleDbConnection(connString)
Dim cmd As New System.Data.OleDb.OleDbDataAdapter(strQuery, cn)
Dim testDataSet As New DataSet()

cmd.Fill(testDataSet)

' Bind DataGrid to the DataSet. DataGrid is the ID for the
' DataGrid control in the HTML section.
Dim source As New DataView(testDataSet.Tables(0))
DataGrid1.DataSource = source
DataGrid1.DataBind()
End Sub
End Class

On the Build menu, click Build Solution.
Back to the top

Run the application

On the Debug menu, click Start to run the application.
Type the query word document in the text box. You can type any word from the IndexText.txt file.
Click Button. Notice that the data grid appears and contains query results from the IndexText.txt file.
Back to the top

Troubleshoot

Wait for the Index to Be Built

Indexing Service automatically processes the TestCatalog and prepares a word list for indexing. When you run the sample code, you may not receive the results as expected because Indexing Service requires some time to build the index. To verify the state of the index, click Indexing Service in the right pane, and then verify that the Word List value for TestCatalog is greater than zero. If the Word List value is zero, wait for the index to build the list.

Index Data May Have Become Corrupted

If the search does not return the expected search results when you query the index, the index data may have become corrupted. To troubleshoot this issue, stop and restart Indexing Service to re-index all documents. To do this, follow these steps:
Click Start, point to Settings, and then click Control Panel.
Double-click Administrative Tools, and then double-click Computer Management.
In the console tree, double-click Services and Applications.
Right-click Indexing Service, and then click Stop.
Right-click Indexing Service, and then click Start.
For more information about Indexing Service, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/ms689644.aspx (http://msdn2.microsoft.com/en-us/library/ms689644.aspx) For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
311521 (http://support.microsoft.com/kb/311521/ ) How to index ASP.NET content by using Microsoft Index Server 308202 (http://support.microsoft.com/kb/308202/ ) How to create and configure a catalog for indexing

From:
http://support.microsoft.com/kb/820105
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐