Strona: [  << <   1   > >>  ]  z  1     
Autor Temat: Serwer SQL
busio
busio




Typ: neutral
Postów: 98
Zarejestrowany: Sep 2002
Serwer SQL

Witam

Potrzebuję wylistować dostępne serwery MSSQL.
Czy zna ktoś metodę aby to osiągnąć?

Pozdrawiam
busio

28-02-2005 10:33
Pokaż profil busio  Wyślij email do busio   Odwiedź stronę busio  
musman
www.codec.pl



Typ: neutral
Postów: 202
Zarejestrowany: Mar 2003

witam
uzywam do tego celu klasy netenum dzieki ktorej moge przeszukac siec i wyodrebnic wszystkie kompy ... oczywiscie na kazdym kompie sprawdzam czy jest serwer sql

oto cala klasa net enum

Imports System
Imports System.Runtime.InteropServices
Public Class NetEnum
    Public Enum RESOURCE_SCOPE
        RESOURCE_Connected = &H1
        RESOURCE_GlobalNET = &H2
        RESOURCE_Remembered = &H3
        RESOURCE_Recent = &H4
        RESOURCE_Context = &H5
    End Enum
    Public Enum RESOURCE_TYPE
        RESOURCETYPE_Any = &H0
        RESOURCETYPE_Disk = &H1
        RESOURCETYPE_Print = &H2
        RESOURCETYPE_Reserved = &H8
    End Enum
    Public Enum RESOURCE_DISPLAYTYPE
        RESOURCEDISPLAYTYPE_Generic = &H0
        RESOURCEDISPLAYTYPE_Domain = &H1
        RESOURCEDISPLAYTYPE_Server = &H2
        RESOURCEDISPLAYTYPE_Share = &H3
        RESOURCEDISPLAYTYPE_File = &H4
        RESOURCEDISPLAYTYPE_Group = &H5
        RESOURCEDISPLAYTYPE_Network = &H6
        RESOURCEDISPLAYTYPE_Root = &H7
        RESOURCEDISPLAYTYPE_ShareAdmin = &H8
        RESOURCEDISPLAYTYPE_Directory = &H9
        RESOURCEDISPLAYTYPE_Tree = &HA
        RESOURCEDISPLAYTYPE_NDSContainer = &HB
    End Enum
    Public Enum RESOURCE_USAGE
        RESOURCEUSAGE_Connectable = &H1
        RESOURCEUSAGE_Container = &H2
        RESOURCEUSAGE_NoLocalDevice = &H4
        RESOURCEUSAGE_Sibling = &H8
        RESOURCEUSAGE_Attached = &H10
        RESOURCEUSAGE_All = RESOURCEUSAGE_Connectable Or _
                            RESOURCEUSAGE_Container Or RESOURCEUSAGE_Attached
    End Enum
    Public Structure NetResource
        Dim Scope As RESOURCE_SCOPE
        Dim Type As RESOURCE_TYPE
        Dim DisplayType As RESOURCE_DISPLAYTYPE
        Dim Usage As RESOURCE_USAGE
        Dim LocalName As String
        Dim RemoteName As String
        Dim Comment As String
        Dim Provider As String
        Dim Level As Byte
    End Structure
    Public Function [Open]() As Boolean
        If Opened Then Call Close()
        WNETOE(Nothing)
        Opened = Not Failed
        Return Opened
    End Function
    Public Function [Next]() As NetResource
        If Not Opened Then Call Open()
        If (RStack.Count > 0) And Opened Then
            Return RStack.Pop
        Else
            Return Nothing
        End If
    End Function
    Public Function [Count]() As Integer
        If Opened Then Return RStack.Count Else Return 0
    End Function
    Public Function [Close]() As Boolean
        RStack.Clear()
        Opened = False
        Return True
    End Function
    Private Structure NETRES
        Dim dwScope As RESOURCE_SCOPE
        Dim dwType As RESOURCE_TYPE
        Dim dwDisplayType As RESOURCE_DISPLAYTYPE
        Dim dwUsage As RESOURCE_USAGE
        <MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
        Dim lpLocalName As String
        <MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
        Dim lpRemoteName As String
        <MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
        Dim lpComment As String
        <MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
        Dim lpProvider As String
    End Structure
    Private Const BufLen As Integer = 32768
    Private Opened As Boolean = False
    Private Failed As Boolean = False
    Private RecLevel As Byte
    Private Ns As NetResource
    Private RStack As New Stack(BufLen / Marshal.SizeOf(Ns))
    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetOpenEnum( _
      ByVal dwScope As RESOURCE_SCOPE, ByVal dwType As RESOURCE_TYPE, _
      ByVal dwUsage As RESOURCE_USAGE, _
      <MarshalAs(UnmanagedType.AsAny), [In]()> ByVal lpNetResource As [Object], _
      ByRef lphEnum As IntPtr) As Integer
    End Function
    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetEnumResource( _
      ByVal hEnum As IntPtr, ByRef lpcCount As Integer, ByVal lpBuffer As IntPtr, _
      ByRef lpBufferSize As Integer) As Integer
    End Function
    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetCloseEnum( _
      ByVal hEnum As IntPtr) As Integer
    End Function
    Private Enum NERR
        NERR_Success = 0
        ERROR_MORE_DATA = 234
        ERROR_NO_BROWSER_SERVERS_FOUND = 6118
        ERROR_INVALID_LEVEL = 124
        ERROR_ACCESS_DENIED = 5
        ERROR_INVALID_PARAMETER = 87
        ERROR_NOT_ENOUGH_MEMORY = 8
        ERROR_NETWORK_BUSY = 54
        ERROR_BAD_NETPATH = 53
        ERROR_NO_NETWORK = 1222
        ERROR_INVALID_HANDLE_STATE = 1609
        ERROR_EXTENDED_ERROR = 1208
    End Enum
    Private Sub WNETOE(ByVal O As [Object])
        Dim iRet As Integer
        Dim Nr As NETRES
        Dim Entries As Integer
        Dim ptrHandle As New IntPtr
        Dim ptrBuffer As IntPtr = Marshal.AllocHGlobal(BufLen)
        RecLevel += 1
        Try
            iRet = WNetOpenEnum(RESOURCE_SCOPE.RESOURCE_GlobalNET, _
              RESOURCE_TYPE.RESOURCETYPE_Any, _
              RESOURCE_USAGE.RESOURCEUSAGE_All, O, ptrHandle)
            If iRet <> 0 Then
                Failed = True
                RecLevel -= 1
                Return
            Else
                Failed = False
            End If
            While True
                Entries = -1
                iRet = WNetEnumResource(ptrHandle, Entries, ptrBuffer, BufLen)
                If iRet <> 0 Or Entries < 1 Then
                    Exit While
                End If
                Dim ptr As Int32 = ptrBuffer.ToInt32()
                Dim i As Integer
                For i = 0 To Entries - 1
                    Nr = CType(Marshal.PtrToStructure(New IntPtr(ptr), GetType(NETRES)), NETRES)
                    If RESOURCE_USAGE.RESOURCEUSAGE_Container = (Nr.dwUsage And RESOURCE_USAGE.RESOURCEUSAGE_Container) Then
                        WNETOE(Nr)
                    End If
                    With Ns
                        .Scope = Nr.dwScope
                        .Type = Nr.dwType
                        .DisplayType = Nr.dwDisplayType
                        .Usage = Nr.dwUsage
                        .LocalName = Nr.lpLocalName
                        .RemoteName = Nr.lpRemoteName
                        .Comment = Nr.lpComment
                        .Provider = Nr.lpProvider
                        .Level = RecLevel
                    End With
                    ptr += Marshal.SizeOf(Nr)
                    RStack.Push(Ns)
                Next i
            End While
            Marshal.FreeHGlobal(ptrBuffer)
            iRet = WNetCloseEnum(ptrHandle)
        Catch e As Exception
        End Try
        RecLevel -= 1
    End Sub
End Class


oraz procedura ktora do listview (serwer_view) dodaje serweri i oznacza czy jest osiagalny mssql lub msde czy nie :

Private Sub lista_serwerow()
        Dim i_lista As Integer = 0
        Dim i As Integer
        Dim iph As IPHostEntry, ips As String
        serwer_view.Visible = False
        If Ne.Open() Then
            Me.Cursor = Cursors.WaitCursor
            For i = 1 To Ne.Count
                Ns = Ne.Next
                ips = ""
                If Ns.DisplayType = NetEnum.RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_Server Then
                    Try
                        iph = Resolve(Ns.RemoteName.Substring(2))
                        ips = iph.AddressList(0).ToString
                    Catch ex As Exception
                        ips = "Niedostępny"
                    End Try
                End If
                If Ns.DisplayType.ToString().Substring(20) = "Server" Then
                    serwer_view.Items.Add(Mid(Ns.RemoteName, 3))
                    If test_mssql(Mid(Ns.RemoteName, 3)) = True Then
                        serwer_view.Items(i_lista).ImageIndex = 0
                        serwer_view.Items(i_lista).SubItems.Add(ips)
                        serwer_view.Items(i_lista).SubItems.Add("OK"
                    Else
                        serwer_view.Items(i_lista).ImageIndex = 1
                        serwer_view.Items(i_lista).SubItems.Add(ips)
                        serwer_view.Items(i_lista).SubItems.Add("N/A"
                    End If
                    i_lista += 1
                End If
            Next i
            Me.Cursor = Cursors.Default
        End If
        Ne.Close()
        UserControl11.stop_anim()
        serwer_view.Visible = True
        Button2.Enabled = True
        Button1.Enabled = True
    End Sub

test_mssql w przypadku tej procedury to funkcja ktora zwraca True jesli moze sie polaczyc z serverem lub False jesli nie moze ... calosc wyglada tak :



oczywiscie klasa netenum posiada funkcje ktore mozesz wywalic ... samo wyszukiwanie i weryfikacja dziala bardzo szybko i fajnie

pozdrawiam

28-02-2005 11:19
Pokaż profil musman  Wyślij email do musman   Odwiedź stronę musman       13495362    3559336
busio
busio




Typ: neutral
Postów: 98
Zarejestrowany: Sep 2002

Jesteś wielki!

Dzięki serdeczne
Pozdrawiam

busio

28-02-2005 14:27
Pokaż profil busio  Wyślij email do busio   Odwiedź stronę busio  
Wszystkich odpowiedzi: 2 :: Maxymalnie na stronę: 20
Strona: [  << <   1   > >>  ]  z  1