Home > July, 2011 存档

VB.net DT distinct 功能

评分 0.00, 满分 5星 0 票 No comments
'*         函数名称:   VB.net DT distinct 功能
'*         摘    要:   用法 dt = SelectDistinct("新DT名", 需要过滤的DT, "字段名")

#Region "DT 临时表 去重复 DT 内 distinct -停止使用"

    Public Function SelectDistinct(ByVal TableName As String, _
                                ByVal SourceTable As DataTable, _
                                ByVal FieldName As String) As DataTable
        Dim dt As New DataTable(TableName)
        dt.Columns.Add(FieldName, SourceTable.Columns(FieldName).DataType)
        Dim dr As DataRow, LastValue As Object
        For Each dr In SourceTable.Select("", FieldName)
            If LastValue Is Nothing OrElse Not ColumnEqual(LastValue, dr(FieldName)) Then
                LastValue = dr(FieldName)
                dt.Rows.Add(New Object() {LastValue})
            End If
        'If Not ds Is Nothing Then ds.Tables.Add(dt)
        Return dt
    End Function

    Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean
        ' Compares two values to determine if they are equal. Also compares DBNULL.Value.
        ' NOTE: If your DataTable contains object fields, you must extend this
        ' function to handle the fields in a meaningful way if you intend to group on them.
        If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both are DBNull.Value.
        If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only one is DBNull.Value.
        Return A = B                                                ' Value type standard comparison
    End Function

#End Region

[原创]VB.net 写入修改Host

评分 0.00, 满分 5星 0 票 No comments
    Imports System.IO
    Imports System.ServiceProcess
     '窗体内 放置 一个RichTextBox 命名为 rtbHost
    Private Function Write_Host(ByVal strIP as string,ByVal strHost as string) As String
        Dim strReturn As String = "OK"
            Dim operachina As String
            Dim strFileName As String

            strFileName = Environ$("SystemRoot") & "\System32\drivers\etc\hosts"

            Dim SR As New StreamReader(strFileName)
            rtbHost.Text = SR.ReadToEnd


            If rtbHost.Find(strHost) < 0 Then

                operachina = strIP + "   " + strHost + vbCrLf

                rtbHost.AppendText(vbCrLf + operachina)

                Dim SW As New StreamWriter(strFileName)

                Dim service As New ServiceController

                service.ServiceName = "Dnscache"

                If service.Status = ServiceControllerStatus.Running Then

                ElseIf service.Status = ServiceControllerStatus.Stopped Then
                End If

            End If

        Catch ex As Exception
            strReturn = ex.Message
        End Try
        Return strReturn
    End Function
     If Write_Host("","test.wuxing.name") <> "OK" Then MsgBox("DNS解析错误,可能无法访问。")
     System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://test.wuxing.name/")