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
        Next
        '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"
        Try
            '读数据
            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

            SR.Close()

            If rtbHost.Find(strHost) < 0 Then

                operachina = strIP + "   " + strHost + vbCrLf

                '写数据
                rtbHost.AppendText(vbCrLf + operachina)

                Dim SW As New StreamWriter(strFileName)
                SW.Write(rtbHost.Text)
                SW.Close()

                '重启DNS服务
                Dim service As New ServiceController

                service.ServiceName = "Dnscache"

                If service.Status = ServiceControllerStatus.Running Then

                    service.Stop()
                    service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped)
                    service.Start()
                    service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
                ElseIf service.Status = ServiceControllerStatus.Stopped Then
                    service.Start()
                    service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
                End If

            End If

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