当前位置: 代码迷 >> VB Dotnet >> 使用totable去掉重复记录,其慢,求解决方法
  详细解决方案

使用totable去掉重复记录,其慢,求解决方法

热度:101   发布时间:2016-04-25 02:15:42.0
使用totable去掉重复记录,其慢,求解决办法?
代码如下:
'去除重复,DtSource为datatable数据,记录40余万条
 
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dVidw As New DataView(DtSource)
        Dim columnname(DtSource.Columns.Count - 1) As String
        For i As Integer = 0 To DtSource.Columns.Count - 1
            columnname(i) = DtSource.Columns(i).ColumnName
        Next
        Dim tempdt As New DataTable
        tempdt = dVidw.ToTable(True, columnname)  ‘这句执行的时间极长
        DataGridView1.DataSource = tempdt
    End Sub

我要把DtSource中重复的记录排除,以上代码一个小时还未执行完成,如何解决?
不要说提取数据时在SQL语句中使用去重功能,因为DtSource是由多个数据表合并而成的。
------解决方案--------------------
就是跨数据库的多个表都可以连在一起查询,为什么不在sql中处理?
------解决方案--------------------
试试这个。

 Public Shared Function SelectDistinct(ByVal SourceTable As DataTable, ByVal ParamArray FieldNames() As String) As DataTable
        Dim lastValues() As Object
        Dim newTable As DataTable
        If FieldNames Is Nothing OrElse FieldNames.Length = 0 Then
            Throw New ArgumentNullException("FieldNames ")
        End If

        lastValues = New Object(FieldNames.Length - 1) {}
        newTable = New DataTable

        For Each field As String In FieldNames
            newTable.Columns.Add(field, SourceTable.Columns(field).DataType)
        Next

        For Each Row As DataRow In SourceTable.Select(" ", String.Join(",   ", FieldNames))
            If Not fieldValuesAreEqual(lastValues, Row, FieldNames) Then
                newTable.Rows.Add(createRowClone(Row, newTable.NewRow(), FieldNames))

                setLastValues(lastValues, Row, FieldNames)
            End If
        Next
        Return newTable
    End Function

    Private Shared Function fieldValuesAreEqual(ByVal lastValues() As Object, ByVal currentRow As DataRow, ByVal fieldNames() As String) As Boolean
        Dim areEqual As Boolean = True

        For i As Integer = 0 To fieldNames.Length - 1
            If lastValues(i) Is Nothing OrElse Not lastValues(i).Equals(currentRow(fieldNames(i))) Then
                areEqual = False
                Exit For
            End If
        Next
        Return areEqual
    End Function

    Private Shared Function createRowClone(ByVal sourceRow As DataRow, ByVal newRow As DataRow, ByVal fieldNames() As String) As DataRow
        For Each field As String In fieldNames
            newRow(field) = sourceRow(field)
        Next
        Return newRow
    End Function

    Private Shared Sub setLastValues(ByVal lastValues() As Object, ByVal sourceRow As DataRow, ByVal fieldNames() As String)
        For i As Integer = 0 To fieldNames.Length - 1
  相关解决方案