创建一个测试文件夹
- 您的计算机 C 驱动器上创建一个新的文件夹。命名文件夹 myCatalogFolder。
- 启动文本编辑器 (如记事本),然后在一个空白文档中粘贴以下文本:
这是测试索引服务器查询测试文档,此文件的名称是 IndexText.text。
- 将文件另存为 C:\myCatalogFolder\IndexText.txt。
创建一个索引服务目录
- 单击 开始,单击 运行,键入 compmgmt.msc,然后单击 确定。
- 展开 服务和应用程序。
- 用鼠标右键单击 索引服务,指向 新建,然后再单击 目录。
- 在 名称 文本框中键入 TestCatalog。
- 单击 浏览 找到要用来将放在目录中,的文件夹,然后单击 确定 两次。
- 在下 新建目录创建,您会收到以下消息: 单击 确定。
- 用鼠标右键单击 索引服务,然后单击 $ 以停止索引服务 停止。
- 用鼠标右键单击 索引服务,然后单击 $ 开始 以重新启动索引服务。
定义目录的作用域创建新编录后,添加想要包括在目录的作用域中的文件夹。范围是包含在目录中并从目录中排除的文件夹集。作用域定义了包含在索引中并被从索引中排除的内容。对于包含或排除的每个文件夹,所有子文件夹都还包括或排除。
- 双击 TestCatalog (在"创建测试文件夹"节中创建该目录)。
- 用鼠标右键单击 目录,指向 新建,然后再单击 目录。
- 单击 浏览,找到然后单击添加 (C:\myCatalogFolder\) 所需的文件夹,然后单击 确定。
注意在下 在索引中的包含吗?,单击 是。
创建 ASP.NET Web 应用程序
- 启动 Microsoft Visual Studio.net。
- 若要创建新的使用 Visual C#.net ASP.NET Web 应用程序 项目。项目 IndexQueryApp 命名。 默认状态下,创建 WebForm1.aspx。
- 在设计视图中用鼠标右键单击 WebForm1,然后单击 查看 HTML 源文件。
- Replace the existing HTML code with following sample code:
- <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="IndexQueryApp.WebForm1" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
- <HTML>
- <HEAD>
- <title>WebForm3</title>
- <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
- <meta name="CODE_LANGUAGE" Content="C#">
- <meta name="vs_defaultClientScript" content="JavaScript">
- <meta name="vs_targetSchema" content="">
- </HEAD>
- <body MS_POSITIONING="GridLayout">
- <form id="WebForm3" method="post" runat="server">
- <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 90px; POSITION: absolute; TOP: 136px" runat="server"></asp:TextBox>
- <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 138px" runat="server"></asp:DataGrid>
- <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 92px; POSITION: absolute; TOP: 199px" runat="server" Text="Button"></asp:Button>
- </form>
- </body>
- </HTML>
- <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="IndexQueryApp.WebForm1" %>
-
在解决方案资源管理器中,用鼠标右键单击 WebForm1,然后单击 查看代码。将现有代码替换下面的代码示例:
- using System;
- using System.Data;
- namespace IndexQueryApp
- {
- public class WebForm1 : System.Web.UI.Page
- {
- protected System.Web.UI.WebControls.TextBox TextBox1;
- protected System.Web.UI.WebControls.DataGrid DataGrid1;
- protected System.Web.UI.WebControls.Button Button1;
-
- private void Page_Load(object sender, System.EventArgs e)
- {
- // Put user code to initialize the page here.
- }
- #region Web Form Designer generated code
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: The ASP.NET Web Form Designer requires this call.
- //
- InitializeComponent();
- base.OnInit(e);
- }
-
- ///
- /// Required method for Designer support - do not modify
- /// the contents of this method by using the code editor.
- ///
- private void InitializeComponent()
- {
- this.Button1.Click += new System.EventHandler(this.Button1_Click);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- private void Button1_Click(object sender, System.EventArgs e)
- {
- // Catalog Name
- string strCatalog = "TestCatalog";
- string strQuery="";
- strQuery = "Select DocTitle,Filename,Size,PATH,URL from Scope() where FREETEXT('" +TextBox1.Text+ "')";
- // TextBox1.Text is the word that you type in the text box to query by using Indexing Service.
- string connstring = "Provider=MSIDXS.1;Integrated Security .='';Data Source="+strCatalog;
- System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connstring);
- conn.Open();
- System.Data.OleDb.OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
-
- System.Data.DataSet testDataSet = new System.Data.DataSet();
-
- cmd.Fill(testDataSet, "SearchResults");
- DataView source = new DataView(testDataSet.Tables[0]);
- DataGrid1.DataSource = source;
- DataGrid1.DataBind();
- }
- }
- }
在 生成 菜单上单击 生成解决方案。
运行该应用程序
- 在 调试 菜单上单击 $ 开始 以运行该应用程序。
- 在文本框中键入查询 word 文档。(您可以键入从 IndexText.txt 文件的任何词)。
- 单击 按钮。请注意数据网格将显示,并且包含查询结果从 IndexText.txt 文件。
疑难解答等待要生成索引$ 索引服务自动处理该 TestCatalog 并准备进行索引的单词列表。您在运行的代码示例时您可能收不到结果,因为索引服务需要一些时间来生成索引的快速。
若要验证的索引状态,启动计算机管理 MMC 管理单元单击右边的窗格中的 索引服务 然后验证 TestCatalog 单词表 值是大于零。如果该 Word 列表 值为零,等待索引以生成列表。 索引数据可能已损坏如果搜索没有返回预期的搜索结果,查询索引时,索引数据可能已损坏。若要此问题的疑难解答停止,然后重新启动索引服务重新编制索引的所有文档。若要这样做,请按照下列步骤操作:
若要验证的索引状态,启动计算机管理 MMC 管理单元单击右边的窗格中的 索引服务 然后验证 TestCatalog 单词表 值是大于零。如果该 Word 列表 值为零,等待索引以生成列表。 索引数据可能已损坏如果搜索没有返回预期的搜索结果,查询索引时,索引数据可能已损坏。若要此问题的疑难解答停止,然后重新启动索引服务重新编制索引的所有文档。若要这样做,请按照下列步骤操作:
- 单击 开始,指向 设置,然后单击 控制面板。
- 双击 管理工具,然后双击 计算机管理。
- 在控制台树中双击 服务和应用程序。
- 用鼠标右键单击 索引服务,然后单击 停止。
- 再次,右键单击 索引服务,然后单击 开始。