Link to home
Start Free TrialLog in
Avatar of deepakyadav
deepakyadav

asked on

how to the know the sql sever name from vb.net and also how to check that the sql server servie is started or not

I am working on a vb.net applications and in that
I want show  the list of all availble sql server name in an combox.
so that user can select one server to which he wants to connect .
( If it is possible with out using any componet then it will be better for me.)

And also I want to know that is sql sever sevice is started or not from vb.net and if not how to start it from vb.net


ASKER CERTIFIED SOLUTION
Avatar of softplus
softplus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A non-component method would be to use the NetServerEnum Win32 APIs to generate a list of servers that have the SQL service runing

Here is an example (that's not exactly what you want, but it demostrates the concepts)

Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.Marshal

Module EnumNet

    'NET_API_STATUS NetServerEnum(
    '  LPCWSTR servername,
    '  DWORD level,
    '  LPBYTE* bufptr,
    '  DWORD prefmaxlen,
    '  LPDWORD entriesread,
    '  LPDWORD totalentries,
    '  DWORD servertype,
    '  LPCWSTR domain,
    '  LPDWORD resume_handle
    ');
    Declare Unicode Function NetServerEnum Lib "Netapi32.dll" ( _
        ByVal servername As Integer, _
        ByVal level As Integer, _
        ByRef bufptr As IntPtr, _
        ByVal prefmaxlen As Integer, _
        ByRef entriesread As Integer, _
        ByRef totalentries As Integer, _
        ByVal servertype As Integer, _
        ByVal domain As String, _
        ByRef resume_handle As Integer) As Integer

    'NET_API_STATUS NetApiBufferFree(
    '  LPVOID Buffer
    ');
    Declare Function NetApiBufferFree Lib "Netapi32.dll" ( _
        ByVal Buffer As IntPtr) As Integer

    'typedef struct _SERVER_INFO_101 {
    '  DWORD sv101_platform_id;
    '  LPWSTR sv101_name;
    '  DWORD sv101_version_major;
    '  DWORD sv101_version_minor;
    '  DWORD sv101_type;
    '  LPWSTR sv101_comment;
    '} SERVER_INFO_101, *PSERVER_INFO_101, *LPSERVER_INFO_101;
    Private Structure Server_info_101
        Dim sv101_platform_id As Integer
        <MarshalAsAttribute(UnmanagedType.LPWStr)> Dim sv101_name As String
        Dim sv101_version_major As Integer
        Dim sv101_version_minor As Integer
        Dim sv101_type As Integer
        <MarshalAsAttribute(UnmanagedType.LPWStr)> Dim sv101_comment As String
    End Structure

    Const SV_TYPE_ALL As Long = &HFFFFFFFF
    Const MAJOR_VERSION_MASK As Long = &HF

    Public MyForm As Form1
    Public Record_count As Integer

    Sub GetEnunNet(ByRef dt As DataTable, ByVal StartDom As String)
        Dim level, MaxLenPref, ret As Integer
        Dim EntriesRead, TotalEntries, ResumeHandle As Integer
        Dim BufPtr, iPtr As IntPtr
        Dim i As Integer
        Dim dr As DataRow
        Dim TempNet As Server_info_101

        ' let's do it!
        MaxLenPref = -1
        level = 101
        ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries, SV_TYPE_ALL, StartDom, ResumeHandle)
        If ret <> 0 Then
            ' record an error message
            Record_count = Record_count + 1
            dr = dt.NewRow
            dr("PCDomain") = StartDom
            dr("PCComment") = "Error! " & ret
            dt.Rows.Add(dr)
        Else
            ' record the results in our database
            For i = 0 To EntriesRead - 1

                ' some wizardry to "walk" the array of NETRESOURCE structures
                iPtr = New IntPtr(BufPtr.ToInt32 + (i * Marshal.SizeOf(TempNet)))
                TempNet = CType(Marshal.PtrToStructure(iPtr, GetType(Server_info_101)), Server_info_101)

                ' add it to our database
                Record_count = Record_count + 1
                dr = dt.NewRow
                dr("PCDomain") = StartDom
                dr("PCName") = TempNet.sv101_name
                dr("OSVersion") = CDbl(CStr(TempNet.sv101_version_major And MAJOR_VERSION_MASK) & "." & CStr(TempNet.sv101_version_minor))
                dr("OSType") = BuildType(TempNet.sv101_type)
                If Not IsNothing(TempNet.sv101_comment) AndAlso TempNet.sv101_comment <> "" Then
                    dr("PCComment") = TempNet.sv101_comment
                End If
                dt.Rows.Add(dr)

                ' keep them entertained
                If i Mod 10 = 0 Then
                    MyForm.Status.Text = StartDom & "\" & TempNet.sv101_name
                    Application.DoEvents()
                End If
            Next
            NetApiBufferFree(BufPtr)
        End If
    End Sub
    Function BuildType(ByVal t As Integer) As String
        Dim Buf As String
        Dim mask As Long
        Dim i As Integer

        If t And &H1000 Then
            Buf = "WinNT, "
        End If
        If t And &H400000 Then
            Buf = "Win9x, "
        End If
        If (t And &H2000) And (Buf <> "Win9x, ") Then
            Buf = "WinFWG, "
        End If

        For i = 0 To 30
            mask = 2 ^ i
            If t And mask Then
                Select Case i
                    Case 2
                        Buf = Buf & "SQL, "
                    Case 3
                        Buf = Buf & "PDC, "
                    Case 4
                        Buf = Buf & "BDC, "
                    Case 5
                        Buf = Buf & "Time, "
                    Case 6
                        Buf = Buf & "Apple, "
                    Case 7
                        Buf = Buf & "Novel, "
                    Case 8
                        Buf = Buf & "Member, "
                    Case 9
                        Buf = Buf & "Print, "
                    Case 10
                        Buf = Buf & "RAS, "
                    Case 11
                        Buf = Buf & "Xenix, "
                    Case 14
                        Buf = Buf & "Netware, "
                    Case 15
                        Buf = Buf & "Server, "
                    Case 18
                        Buf = Buf & "Browse, "
                End Select
            End If
        Next

        ' zap the trailing comma and space
        If Len(Buf) > 2 Then
            Buf = Left(Buf, Len(Buf) - 2)
        End If

        Return Buf
    End Function
End Module
Avatar of gopinathdeepak
gopinathdeepak

ok...

for starting sql server or to get the status of the server (is it running or not), u can use the "scm" command...it is in the C:\Program Files\Microsoft SQL Server\80\Tools\Binn directory...

u have to use a shell execution using Shell("scm -parameters...") or by using the Process class..

just go to cmd prompt and run "scm /?"..it is a powerful tool...

----another suggestion is ...ur requirement looks very similar to theSQL service manager utility..(command is sqlmangr.exe...in the same directory)...usually the installation makes a PATH entry for this folder..so u dont have to navigate to the microsoft sql server folder to run the command..u can run it from anywhere...it is global...

so, i suggest simply call the sqlmangr.exe command from the VB Shell or Process class...it suits ur needs perfectly..

to test, simply go to command prompt and run sqlmangr.exe

tell me if this is what u wanted...

hope it helps

u know...i am wrong if u dont have sql server on the machine u are using...sorry about that

;)