' 绑定查询结果 PrivateSub BindSearch()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 AsInteger=0 dbAdapter.SelectCommand = oleDbSelectCommand1 oleDbSelectCommand1.Connection = dbConnection dbConnection.ConnectionString ="Provider=MSIDXS.1;Integrated Security .='';Data Source=Web" Try dbAdapter.SelectCommand.CommandText =Command IfCStr(ViewState("KEY")) <>""Then dbAdapter.Fill(ds) rows = ds.Tables(0).Rows.Count Else lblResultCount.Text ="请输入关键词进行查询!" EndIf IfNot ds IsNothingAndAlso rows >0Then ' 自定义分页,与查询逻辑无关 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() EndIf Catch ex As Exception lblResultCount.ForeColor = Color.Red lblResultCount.Text =String.Format("无法执行特定的查询: {0}", ex.Message) dgResultsGrid.Visible =False Finally lblResultCount.Visible =True EndTry ' 自定义分页 BindNavigate() End Sub
PrivateReadOnlyProperty Command()PropertyCommand() AsString Get Dim query AsString 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 AsString=CStr(ViewState("TYPE")).ToLower() Dim fmt AsString=" AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))" Dim text AsString=CStr(ViewState("KEY")).Replace(";", "").Trim If type ="all"Or type ="any"Or type ="boolean"Then Dim words() AsString=Split(text, "") DimlenAsInteger= words.Length Dim i AsInteger For i =0Tolen-1Step i +1 Dim word AsString= words(i) If type ="boolean"AndAlso (String.Compare(word, "and", True) =0OrElseString.Compare(word, "or", True) =0OrElseString.Compare(word, "not", True) =0OrElseString.Compare(word, "near", True) =0) Then ElseIf word <>""Then words(i) =String.Format("""{0}""", word) If i <len-1Then If type ="all"Then words(i) +=" AND" ElseIf type ="any"Then words(i) +=" OR" EndIf EndIf EndIf 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) EndIf query +=" ORDER BY Rank DESC" Return query EndGet End Property
' 如果网页没有Title,那么使用文件名 ProtectedFunction GetTitle()Function GetTitle(ByVal value AsObject) AsObject Dim title AsString= Convert.ToString(DataBinder.Eval(value, "DocTitle")) IfNot (title IsNothing) And title.Length >0Then Return title ' Else Return DataBinder.Eval(value, "Filename") EndIf End Function'GetTitle ' 取摘要 ProtectedFunction GetCharacterization()Function GetCharacterization(ByVal value AsObject) AsString Return Server.HtmlEncode(Convert.ToString(DataBinder.Eval(value, "Characterization"))) End Function ' 取文件尺寸,单位KB ProtectedFunction GetFileSize()Function GetFileSize(ByVal value AsObject) AsString Return Convert.ToString(CInt(Convert.ToInt32(DataBinder.Eval(value, "Size")) /1000)) End Function