自定义DataTable搜索

时间:2009-04-14 14:56:57

标签: vb.net .net-2.0 datatable dataset

编辑:注意。我应该提到我对使用.Select,DataRowView,RowFind等不感兴趣。感谢您提供这些建议,但代码原样,我希望能够优化。

我编写了一个函数来搜索数据表中的数组中的术语。然后我将数据表作为数据集返回,以便它可以绑定到DataGridView。有没有人有任何优化想法?由于这是一个实时搜索(每个术语输入即时搜索),我想尽快使功能。速度实际上已经相当不错但我觉得如果还有什么我可以改变的话,我会值得你问一下。 的

  • VB.NET
  • .NET 2.0

    公共函数SearchEngine(ByVal Terms()As String,ByRef ResidentTBL As DataTable)As DataSet             Dim newdts As New DataSet(“用户”)             昏暗的newtable,temptable As New DataTable             Dim residentPassCeiling,tempPassCeiling,termsceiling As Integer             residentPassCeiling = ResidentTBL.Rows.Count - 1             termsceiling = Terms.GetUpperBound(0)             newtable = ResidentTBL.Clone

        For j = 0 To termsceiling

            If j < 1 Then
                '######################## BEGIN First Pass ########################################################################
                For i = 0 To residentPassCeiling

                    If ResidentTBL.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper = Terms(j).ToUpper Then
                        'we have an excact match on account skip further additions
                        newtable.Rows.Clear()
                        newtable.ImportRow(ResidentTBL.Rows(i))
                        newdts.Tables.Add(newtable)
                        Return newdts
                    Else
                        If _
                        ResidentTBL.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.displayName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.sn - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.givenName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.l - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.department - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.telephoneNumber - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        ResidentTBL.Rows(i).Item(TheColumns.building - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Then
                            'then add to the row
                            newtable.ImportRow(ResidentTBL.Rows(i))
                        End If
                    End If

                Next i
                '######################## END First Pass ########################################################################
            Else
                '######################## BEGIN Second.. Pass(es) ########################################################################
                'Further searches we want to search the already filtered datatable from each term
                temptable = newtable.Clone
                temptable = newtable.Copy
                newtable.Clear()
                tempPassCeiling = temptable.Rows.Count - 1
                For i = 0 To tempPassCeiling

                    If temptable.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper = Terms(j).ToUpper Then
                        'we have an excact match on account skip further additions
                        newtable.Rows.Clear()
                        newtable.ImportRow(temptable.Rows(i))
                        newdts.Tables.Add(newtable)
                        Return newdts
                    Else
                        If _
                        temptable.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        temptable.Rows(i).Item(TheColumns.sn - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        temptable.Rows(i).Item(TheColumns.givenName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        temptable.Rows(i).Item(TheColumns.l - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        temptable.Rows(i).Item(TheColumns.department - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        temptable.Rows(i).Item(TheColumns.telephoneNumber - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
                        temptable.Rows(i).Item(TheColumns.building - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Then
                            'then add to the row
                            newtable.ImportRow(temptable.Rows(i))
                        End If
                    End If

                Next i

                temptable = Nothing
                '######################## END Second.. Pass(es) ########################################################################
            End If

        Next j
        newdts.Tables.Add(newtable)
        ResidentTBL = Nothing

        Return newdts
        newdts = Nothing

    End Function

2 个答案:

答案 0 :(得分:2)

您可能希望使用DataViews来过滤数据:

http://aspnet101.com/aspnet101/tutorials.aspx?id=31

答案 1 :(得分:0)

您可以生成DataTable过滤器字符串以从数据表中选择行。

http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx

以下是过滤字符串

的文档

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx