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

Posted on 2005-05-14
Last Modified: 2010-04-23
I am working on a 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 and if not how to start it from

Question by:deepakyadav
    LVL 13

    Accepted Solution

    Use SQLDMO ( ):

    assuming cboServers = Combobox

    dim app as SQLDMO.Application
    app = new SQLDMO.ApplicationClass
    dim names as SQLDMO.NameList
    names = app.ListAvailableSQLServers()
    dim i%
    for i = 1 to names.Count
       cboServers.Items.Add names.Item(i)

    ( )
    LVL 41

    Expert Comment

    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
                ' 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

                    ' keep them entertained
                    If i Mod 10 = 0 Then
                        MyForm.Status.Text = StartDom & "\" & TempNet.sv101_name
                    End If
            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

            ' 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
    LVL 4

    Expert Comment


    for starting sql server or to get the status of the server (is it running or not), u can use the "scm" 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 /?" is a powerful tool...

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

    so, i suggest simply call the sqlmangr.exe command from the VB Shell or Process 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

    LVL 4

    Expert Comment

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


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now