Create an index and search for a database using e.net. 2.

Source: Internet
Author: User
Tags createindex
In fact, it is very easy to create indexes for the database in paie.net. You only need to read the records in the data table and then index each field. In this article, the database content is a blog table-userblog table.

1. Table Structure:
Field Name field type field meaning
Id varchar (11) No.
Title varchar (50) Title
Content text content

2. Procedure
1) Open the database;
2) create an index;
3) perform full-text search based on the index.

4. Source Code:
Aspx file:
<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "webapplication4.webform1" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> Use cmde.net to create a simple database search program </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Table width = "100%" border = "0">
<Tr>
<TD> & nbsp;
<Asp: textbox id = "TJ" runat = "server"> </ASP: textbox> <asp: button id = "Search" runat = "server" text = "Search"> </ASP: button> </TD>
</Tr>
</Table>
<Table width = "100%" border = "0">
<Tr>
<TD> <asp: DataGrid id = "seargrid" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: templatecolumn>
<Headertemplate>
Search Results:
</Headertemplate>
<Itemtemplate>
<Table width = "100%" border = "0">
<Tr>
<TD> ID: <% # databinder. eval (container. dataitem, "ID") %>
</TD>
</Tr>
<Tr>
<TD> title:
<% # Databinder. eval (container. dataitem, "title") %>
</TD>
</Tr>
<Tr>
<TD> content:
<% # Databinder. eval (container. dataitem, "content") %>
</TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

CS code

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using Lucene. net;
Using Lucene. net. index;
Using Lucene. net. documents;
Using Lucene. net. queryparsers;
Using Lucene. net. search;
Using Lucene. net. analysis. Standard;
Using Lucene. net. analysis. cn;

Namespace webapplication4
{
/** // <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox TJ;
Protected system. Web. UI. webcontrols. Button search;
Protected system. Web. UI. webcontrols. DataGrid seargrid;

Public String connstr = "Server =.; database = topwin2; uid = sa; Pwd = ";
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! Page. ispostback)
{
// Open the database table
Sqldatareader myred = OpenTable ();
// Create an index
Indexwriter writer = createindex (myred );
}
}

Public sqldatareader OpenTable ()
{
Sqlconnection mycon = new sqlconnection (connstr );
Mycon. open ();
Sqlcommand mycom = new sqlcommand ("select ID, title, content from userblog order by ID", mycon );
Return mycom. executereader ();
}

Public indexwriter createindex (sqldatareader myred)
{
Indexwriter writer = new indexwriter ("C:/index/", new chineseanalyzer (), true );
Try
{
// Create an index Field
While (myred. Read ())
{
Document Doc = new document ();
Doc. Add (field. Keyword ("ID", myred ["ID"]. tostring ()));
Doc. Add (field. Text ("title", myred ["title"]. tostring ()));
Doc. Add (field. Text ("content", myred ["content"]. tostring ()));
Writer. adddocument (DOC );

}
Writer. Optimize ();
Writer. Close ();
}
Catch (exception E)
{
Response. Write (E );
}
Return writer;
}

Public hits seacher (string querystring)
{
Hits hits = NULL;
Try
{
Indexsearcher mysea = new indexsearcher ("C:/index /");
Query query = queryparser. parse (querystring, "content", new chineseanalyzer ());
Hits = mysea. Search (query );
}
Catch (exception E)
{
Response. Write (E );
}
Return hits;
}

Code generated by web form designer # code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Search. Click + = new system. eventhandler (this. search_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void search_click (Object sender, system. eventargs E)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.