|
-
Feb 23rd, 2005, 02:14 PM
#1
Thread Starter
Addicted Member
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...
-
Feb 23rd, 2005, 03:27 PM
#2
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:
Option Explicit
'Add a reference to M$ SQLDMO Object Library.
Private Sub Command1_Click()
Dim oSQLApp As SQLDMO.Application
Dim oNames As SQLDMO.NameList
Dim i as integer
Set oSQLApp = New SQLDMO.Application
'POPULATE CBO ONLY WITH SQL SERVERS
Set oNames = oSQLApp.ListAvailableSQLServers
cboServer.Clear
For i = 1 To oNames.Count
cboServer.AddItem oNames.Item(i)
Next i
If oNames.Count = 0 Then
cboServer.AddItem "NO SQL SERVERS FOUND"
EndIf
End Sub
Private Sub Command2_Click()
Set goSQL = New SQLDMO.SQLServer
goSQL.Connect CStr(cboServer), CStr(txtLoginName), CStr (txtPassword) 'oSQL.HostName
If goSQL.ConnectionID <= 0 Then
MessageBox.Show("CONNECTION FAILED!!!")
Exit Sub
End If
sRc = goSQL.ExecuteWithResults("USE master SELECT * FROM sysdatabases", SQLDMOExec_Default).Rows
sRs = " "
i = 0
cboDatabase.Clear
Do Until i = sRc
i = i + 1
sRs = goSQL.ExecuteWithResults("USE master SELECT * FROM sysdatabases", SQLDMOExec_Default).GetColumnString(i, 1)
cboDatabase.AddItem sRs
Loop
cboDatabase = cboDatabase.List(0)
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 5th, 2006, 03:01 AM
#3
PowerPoster
Re: Listing of SQL Servers
 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:
Option Explicit
'Add a reference to M$ SQLDMO Object Library.
Private Sub Command1_Click()
Dim oSQLApp As SQLDMO.Application
Dim oNames As SQLDMO.NameList
Dim i as integer
Set oSQLApp = New SQLDMO.Application
'POPULATE CBO ONLY WITH SQL SERVERS
Set oNames = oSQLApp.ListAvailableSQLServers
cboServer.Clear
For i = 1 To oNames.Count
cboServer.AddItem oNames.Item(i)
Next i
If oNames.Count = 0 Then
cboServer.AddItem "NO SQL SERVERS FOUND"
EndIf
End Sub
Private Sub Command2_Click()
Set goSQL = New SQLDMO.SQLServer
goSQL.Connect CStr(cboServer), CStr(txtLoginName), CStr (txtPassword) 'oSQL.HostName
If goSQL.ConnectionID <= 0 Then
MessageBox.Show("CONNECTION FAILED!!!")
Exit Sub
End If
sRc = goSQL.ExecuteWithResults("USE master SELECT * FROM sysdatabases", SQLDMOExec_Default).Rows
sRs = " "
i = 0
cboDatabase.Clear
Do Until i = sRc
i = i + 1
sRs = goSQL.ExecuteWithResults("USE master SELECT * FROM sysdatabases", SQLDMOExec_Default).GetColumnString(i, 1)
cboDatabase.AddItem sRs
Loop
cboDatabase = cboDatabase.List(0)
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.
-
Jun 5th, 2006, 06:39 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 5th, 2006, 08:28 AM
#5
PowerPoster
Re: Listing of SQL Servers
 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?
-
Jun 5th, 2006, 08:42 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|