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

Posted on 2005-05-14
Medium Priority
Last Modified: 2010-04-23
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

Question by:deepakyadav
  • 2
LVL 13

Accepted Solution

softplus earned 120 total points
ID: 14002261
Use SQLDMO ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqldmo/dmoref_con03_8q44.asp ):

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)

( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqldmo/dmoref_p_s_769l.asp )
LVL 41

Expert Comment

ID: 14003328
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;
    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

Expert Comment

ID: 14007844

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


Expert Comment

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


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

840 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