Results 1 to 6 of 6

Thread: Listing of SQL Servers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    131

    Listing of SQL Servers

    I am writing a program in VB6 where the user can select the sql server name and the database. Using ADO how would I get a dropdown of available servers and their databases (for the user to select during runtime)

    Thanks
    Things fall apart which the center cannot hold...

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listing of SQL Servers

    If you have the SQL CLient tools installed on your system then you will have the SQLDMO Object library which you can use to do it.
    VB Code:
    1. Option Explicit
    2. 'Add a reference to M$ SQLDMO Object Library.
    3. Private Sub Command1_Click()
    4.  
    5.     Dim oSQLApp As SQLDMO.Application
    6.     Dim oNames As SQLDMO.NameList
    7.     Dim i as integer
    8.  
    9.     Set oSQLApp = New SQLDMO.Application
    10.     'POPULATE CBO ONLY WITH SQL SERVERS
    11.     Set oNames = oSQLApp.ListAvailableSQLServers
    12.     cboServer.Clear
    13.     For i = 1 To oNames.Count
    14.         cboServer.AddItem oNames.Item(i)
    15.     Next i
    16.     If oNames.Count = 0 Then
    17.         cboServer.AddItem "NO SQL SERVERS FOUND"
    18.     EndIf
    19.  
    20. End Sub
    21.  
    22. Private Sub Command2_Click()
    23.  
    24.     Set goSQL = New SQLDMO.SQLServer
    25.     goSQL.Connect CStr(cboServer), CStr(txtLoginName), CStr (txtPassword) 'oSQL.HostName
    26.     If goSQL.ConnectionID <= 0 Then
    27.         MessageBox.Show("CONNECTION FAILED!!!")
    28.         Exit Sub
    29.     End If
    30.     sRc = goSQL.ExecuteWithResults("USE master SELECT * FROM     sysdatabases", SQLDMOExec_Default).Rows
    31.     sRs = " "
    32.     i = 0
    33.     cboDatabase.Clear
    34.     Do Until i = sRc
    35.         i = i + 1
    36.         sRs = goSQL.ExecuteWithResults("USE master SELECT * FROM sysdatabases", SQLDMOExec_Default).GetColumnString(i, 1)
    37.         cboDatabase.AddItem sRs
    38.     Loop
    39.     cboDatabase = cboDatabase.List(0)
    40.  
    41. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,943

    Re: Listing of SQL Servers

    Quote Originally Posted by RobDog888
    If you have the SQL CLient tools installed on your system then you will have the SQLDMO Object library which you can use to do it.
    VB Code:
    1. Option Explicit
    2. 'Add a reference to M$ SQLDMO Object Library.
    3. Private Sub Command1_Click()
    4.  
    5.     Dim oSQLApp As SQLDMO.Application
    6.     Dim oNames As SQLDMO.NameList
    7.     Dim i as integer
    8.  
    9.     Set oSQLApp = New SQLDMO.Application
    10.     'POPULATE CBO ONLY WITH SQL SERVERS
    11.     Set oNames = oSQLApp.ListAvailableSQLServers
    12.     cboServer.Clear
    13.     For i = 1 To oNames.Count
    14.         cboServer.AddItem oNames.Item(i)
    15.     Next i
    16.     If oNames.Count = 0 Then
    17.         cboServer.AddItem "NO SQL SERVERS FOUND"
    18.     EndIf
    19.  
    20. End Sub
    21.  
    22. Private Sub Command2_Click()
    23.  
    24.     Set goSQL = New SQLDMO.SQLServer
    25.     goSQL.Connect CStr(cboServer), CStr(txtLoginName), CStr (txtPassword) 'oSQL.HostName
    26.     If goSQL.ConnectionID <= 0 Then
    27.         MessageBox.Show("CONNECTION FAILED!!!")
    28.         Exit Sub
    29.     End If
    30.     sRc = goSQL.ExecuteWithResults("USE master SELECT * FROM     sysdatabases", SQLDMOExec_Default).Rows
    31.     sRs = " "
    32.     i = 0
    33.     cboDatabase.Clear
    34.     Do Until i = sRc
    35.         i = i + 1
    36.         sRs = goSQL.ExecuteWithResults("USE master SELECT * FROM sysdatabases", SQLDMOExec_Default).GetColumnString(i, 1)
    37.         cboDatabase.AddItem sRs
    38.     Loop
    39.     cboDatabase = cboDatabase.List(0)
    40.  
    41. End Sub
    OK TKS !!!
    Work but have error(on the line with arrow), if you see the image attached.
    Last edited by luca90; Jan 11th, 2009 at 06:35 AM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listing of SQL Servers

    Does your login have permissions in SQL?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,943

    Re: Listing of SQL Servers

    Quote Originally Posted by RobDog888
    Does your login have permissions in SQL?
    I dont know.... but assuming i wolud want to know only the name of server have SQL, need a password?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listing of SQL Servers

    Well if you just want to list the SQL Servers on your network then no. You dont need a sql login to list the servers but you will if you want to list the databases on a particular sql server.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width