How to get a list of SQL Server Instances?

P

Peter

I'm trying to get a list of SQL Server Instances thru a VB.NET application.
I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe
from http://www.sqldev.net/misc/ListSQLSvr.htm

I run all of them on a Windows XP Professional 64bit machine which has 2
instances: mymachine\SQLExpress (Express Edition) and MSSQLSERVER (Developer
Edition (64bit))

SQL Browser Service: not running

- GetDataSource or SMO returns MSSQLSERVER
- ListSQLSvr returns nothing

SQL Browser Service: running

- GetDataSource or SMO or ListSQLSvr returns mymachine\SQLEXPRESS
 
P

Peter

Additional Info:

If I try to add connection in Server Explorer of VS2008,

- SQL Browser Service: not running
only see MSSQLSERVER

- SQL Browser Service: running
only see mymachine\SQLEXPRESS

So, this behavior is same as GetDataSource or SMO.

I hope someone can explain this strange behavior and tell me how I should
get the list of SQL Server Instances in a VB.Net application.
 
T

Tom Shelton

Additional Info:

If I try to add connection in Server Explorer of VS2008,

- SQL Browser Service: not running
only see MSSQLSERVER

- SQL Browser Service: running
only see mymachine\SQLEXPRESS

So, this behavior is same as GetDataSource or SMO.

I hope someone can explain this strange behavior and tell me how I should
get the list of SQL Server Instances in a VB.Net application.










- Show quoted text -

Here's some C# code I wrote for a dialog box component that mimics the
one from SQL Management Studio... It seems to work for me:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Data;
using System.Data.Sql;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;

namespace FireAnt.Controls.Database
{
[Serializable()]
internal class ServerInstance : IComparable,
IComparable<ServerInstance>, IComparable<string>
{
private string server;
private string instance;
private Version version;

private static readonly string LocalServer = Dns.GetHostName
().ToUpper ();
private const string ServerInstanceRegistryKey = "SOFTWARE\
\MICROSOFT\\MICROSOFT SQL SERVER\\INSTANCE NAMES\\SQL";

#region Constructors
public ServerInstance () : this ( string.Empty ) { }
public ServerInstance ( string server ) : this ( server,
string.Empty ) { }
public ServerInstance ( string server, string instance ) :
this ( server, instance, "0.0" ) { }
public ServerInstance ( string server, string instance, string
version ) : this ( server, instance, new Version ( version ) ) { }
public ServerInstance ( string server, string instance,
Version version )
{
this.server = server;
this.instance = instance;
this.version = version;
}
#endregion

#region Properties
public string Server
{
get { return this.server; }
}

public string Instance
{
get { return this.instance; }
}

public Version Version
{
get { return this.version; }
}
#endregion

#region Overrides
public override string ToString ()
{
StringBuilder displayString = new StringBuilder
( this.Server );

// add the instance name if we have one
if ( !string.IsNullOrEmpty ( this.Instance ) )
{
displayString.AppendFormat ( "\\{0}", this.Instance );
}

// add the version if it is a valid one
if ( this.Version.Major != 0 )
{
displayString.AppendFormat ( " ({0}.{1})",
this.Version.Major, this.Version.Minor );
}

return displayString.ToString ();
}

public override bool Equals ( object obj )
{
if ( obj == null || !( obj is ServerInstance ) )
return false;
return ( this.ToString () == obj.ToString () );
}

public override int GetHashCode ()
{
return this.ToString ().GetHashCode ();
}
#endregion

#region Operators
public static implicit operator string ( ServerInstance
instance )
{
return instance.ToString ();
}
#endregion

public static List<ServerInstance> GetLocalServerList ()
{
List<ServerInstance> localServers = new
List<ServerInstance> ();

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey
( ServerInstanceRegistryKey );
if ( registryKey != null )
{
using ( registryKey )
{
foreach ( string instanceName in
registryKey.GetValueNames () )
{
localServers.Add ( new ServerInstance
( LocalServer, instanceName ) );
}
}
}

return localServers;
}

public static List<ServerInstance> GetNetworkServerList ()
{
List<ServerInstance> networkServers = new
List<ServerInstance> ();

using ( DataTable dataSources =
SqlDataSourceEnumerator.Instance.GetDataSources () )
{
foreach ( DataRow dataSource in dataSources.Rows )
{
networkServers.Add ( new ServerInstance (
(string)dataSource["ServerName"],
Convert.IsDBNull
( dataSource["InstanceName"] ) ? string.Empty :
(string)dataSource["InstanceName"],
Convert.IsDBNull ( dataSource["Version"] ) ?
"0.0" : (string)dataSource["Version"] ) );
}
}

return networkServers;
}


#region IComparable Members

public int CompareTo ( object obj )
{
if ( obj == null || !(obj is ServerInstance ))
return -1;
else
return this.ToString ().CompareTo ( obj.ToString () );

}

#endregion

#region IComparable<ServerInstance> Members

public int CompareTo ( ServerInstance other )
{
return this.ToString ().CompareTo ( other.ToString () );
}

#endregion

#region IComparable<string> Members

public int CompareTo ( string other )
{
return this.ToString ().CompareTo ( other );
}

#endregion
}
}

Anyway, maybe you can make use of this - convert it to vb or
whatever :)
 
P

Peter

Hi Tom,

Thanks for the code and will try it. If I understand your coding correctly,
you're checking local registries for local instances and using GetDataSource
for network instances. My current coding is just using GetDataSource.

The issue that I'm experiencing seems to be the one mentioned in here:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=198078&SiteID=1
I think your coding may not work for this known issue.


Peter

Tom Shelton said:
Additional Info:

If I try to add connection in Server Explorer of VS2008,

- SQL Browser Service: not running
only see MSSQLSERVER

- SQL Browser Service: running
only see mymachine\SQLEXPRESS

So, this behavior is same as GetDataSource or SMO.

I hope someone can explain this strange behavior and tell me how I should
get the list of SQL Server Instances in a VB.Net application.










- Show quoted text -

Here's some C# code I wrote for a dialog box component that mimics the
one from SQL Management Studio... It seems to work for me:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Data;
using System.Data.Sql;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;

namespace FireAnt.Controls.Database
{
[Serializable()]
internal class ServerInstance : IComparable,
IComparable<ServerInstance>, IComparable<string>
{
private string server;
private string instance;
private Version version;

private static readonly string LocalServer = Dns.GetHostName
().ToUpper ();
private const string ServerInstanceRegistryKey = "SOFTWARE\
\MICROSOFT\\MICROSOFT SQL SERVER\\INSTANCE NAMES\\SQL";

#region Constructors
public ServerInstance () : this ( string.Empty ) { }
public ServerInstance ( string server ) : this ( server,
string.Empty ) { }
public ServerInstance ( string server, string instance ) :
this ( server, instance, "0.0" ) { }
public ServerInstance ( string server, string instance, string
version ) : this ( server, instance, new Version ( version ) ) { }
public ServerInstance ( string server, string instance,
Version version )
{
this.server = server;
this.instance = instance;
this.version = version;
}
#endregion

#region Properties
public string Server
{
get { return this.server; }
}

public string Instance
{
get { return this.instance; }
}

public Version Version
{
get { return this.version; }
}
#endregion

#region Overrides
public override string ToString ()
{
StringBuilder displayString = new StringBuilder
( this.Server );

// add the instance name if we have one
if ( !string.IsNullOrEmpty ( this.Instance ) )
{
displayString.AppendFormat ( "\\{0}", this.Instance );
}

// add the version if it is a valid one
if ( this.Version.Major != 0 )
{
displayString.AppendFormat ( " ({0}.{1})",
this.Version.Major, this.Version.Minor );
}

return displayString.ToString ();
}

public override bool Equals ( object obj )
{
if ( obj == null || !( obj is ServerInstance ) )
return false;
return ( this.ToString () == obj.ToString () );
}

public override int GetHashCode ()
{
return this.ToString ().GetHashCode ();
}
#endregion

#region Operators
public static implicit operator string ( ServerInstance
instance )
{
return instance.ToString ();
}
#endregion

public static List<ServerInstance> GetLocalServerList ()
{
List<ServerInstance> localServers = new
List<ServerInstance> ();

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey
( ServerInstanceRegistryKey );
if ( registryKey != null )
{
using ( registryKey )
{
foreach ( string instanceName in
registryKey.GetValueNames () )
{
localServers.Add ( new ServerInstance
( LocalServer, instanceName ) );
}
}
}

return localServers;
}

public static List<ServerInstance> GetNetworkServerList ()
{
List<ServerInstance> networkServers = new
List<ServerInstance> ();

using ( DataTable dataSources =
SqlDataSourceEnumerator.Instance.GetDataSources () )
{
foreach ( DataRow dataSource in dataSources.Rows )
{
networkServers.Add ( new ServerInstance (
(string)dataSource["ServerName"],
Convert.IsDBNull
( dataSource["InstanceName"] ) ? string.Empty :
(string)dataSource["InstanceName"],
Convert.IsDBNull ( dataSource["Version"] ) ?
"0.0" : (string)dataSource["Version"] ) );
}
}

return networkServers;
}


#region IComparable Members

public int CompareTo ( object obj )
{
if ( obj == null || !(obj is ServerInstance ))
return -1;
else
return this.ToString ().CompareTo ( obj.ToString () );

}

#endregion

#region IComparable<ServerInstance> Members

public int CompareTo ( ServerInstance other )
{
return this.ToString ().CompareTo ( other.ToString () );
}

#endregion

#region IComparable<string> Members

public int CompareTo ( string other )
{
return this.ToString ().CompareTo ( other );
}

#endregion
}
}

Anyway, maybe you can make use of this - convert it to vb or
whatever :)
 
T

Tom Shelton

Hi Tom,

Thanks for the code and will try it. If I understand your coding correctly,
you're checking local registries for local instances and using GetDataSource
for network instances. My current coding is just using GetDataSource.

The issue that I'm experiencing seems to be the one mentioned in here:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=198078&SiteID=1
I think your coding may not work for this known issue.

Peter



Here's some C# code I wrote for a dialog box component that mimics the
one from SQL Management Studio... It seems to work for me:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Data;
using System.Data.Sql;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;
namespace FireAnt.Controls.Database
{
[Serializable()]
internal class ServerInstance : IComparable,
IComparable<ServerInstance>, IComparable<string>
{
private string server;
private string instance;
private Version version;
private static readonly string LocalServer = Dns.GetHostName
().ToUpper ();
private const string ServerInstanceRegistryKey = "SOFTWARE\
\MICROSOFT\\MICROSOFT SQL SERVER\\INSTANCE NAMES\\SQL";
#region Constructors
public ServerInstance () : this ( string.Empty ) { }
public ServerInstance ( string server ) : this ( server,
string.Empty ) { }
public ServerInstance ( string server, string instance ) :
this ( server, instance, "0.0" ) { }
public ServerInstance ( string server, string instance, string
version ) : this ( server, instance, new Version ( version ) ) { }
public ServerInstance ( string server, string instance,
Version version )
{
this.server = server;
this.instance = instance;
this.version = version;
}
#endregion
#region Properties
public string Server
{
get { return this.server; }
}
public string Instance
{
get { return this.instance; }
}
public Version Version
{
get { return this.version; }
}
#endregion
#region Overrides
public override string ToString ()
{
StringBuilder displayString = new StringBuilder
( this.Server );
// add the instance name if we have one
if ( !string.IsNullOrEmpty ( this.Instance ) )
{
displayString.AppendFormat ( "\\{0}", this.Instance );
}
// add the version if it is a valid one
if ( this.Version.Major != 0 )
{
displayString.AppendFormat ( " ({0}.{1})",
this.Version.Major, this.Version.Minor );
}
return displayString.ToString ();
}
public override bool Equals ( object obj )
{
if ( obj == null || !( obj is ServerInstance ) )
return false;
return ( this.ToString () == obj.ToString () );
}
public override int GetHashCode ()
{
return this.ToString ().GetHashCode ();
}
#endregion
#region Operators
public static implicit operator string ( ServerInstance
instance )
{
return instance.ToString ();
}
#endregion
public static List<ServerInstance> GetLocalServerList ()
{
List<ServerInstance> localServers = new
List<ServerInstance> ();
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey
( ServerInstanceRegistryKey );
if ( registryKey != null )
{
using ( registryKey )
{
foreach ( string instanceName in
registryKey.GetValueNames () )
{
localServers.Add ( new ServerInstance
( LocalServer, instanceName ) );
}
}
}
return localServers;
}
public static List<ServerInstance> GetNetworkServerList ()
{
List<ServerInstance> networkServers = new
List<ServerInstance> ();
using ( DataTable dataSources =
SqlDataSourceEnumerator.Instance.GetDataSources () )
{
foreach ( DataRow dataSource in dataSources.Rows )
{
networkServers.Add ( new ServerInstance (
(string)dataSource["ServerName"],
Convert.IsDBNull
( dataSource["InstanceName"] ) ? string.Empty :
(string)dataSource["InstanceName"],
Convert.IsDBNull ( dataSource["Version"] ) ?
"0.0" : (string)dataSource["Version"] ) );
}
}
return networkServers;
}
#region IComparable Members
public int CompareTo ( object obj )
{
if ( obj == null || !(obj is ServerInstance ))
return -1;
else
return this.ToString ().CompareTo ( obj.ToString () );


#region IComparable<ServerInstance> Members
public int CompareTo ( ServerInstance other )
{
return this.ToString ().CompareTo ( other.ToString () );
}

#region IComparable<string> Members
public int CompareTo ( string other )
{
return this.ToString ().CompareTo ( other );
}

Anyway, maybe you can make use of this - convert it to vb or
whatever :)

- Show quoted text -

Actually, it seems to work fine here. I have two instances on this
box - the default (it shows up as MSSQLSERVER), and then an named
instance os sql express - SQLEXPRESS.

I also have default on another PC, and it is found also. Basically,
mine matches whats in the management studio dialog - except that in
management studio, the default local instance doesn't show
MSSQLSERVER, mine does.
 
P

Peter

Hi Kerry,

I have also tried SQLDataSourceEnumerator class and have the same problem.

Peter
 
P

Peter

Hi Tom,

If you run your coding on another PC, does it show the default instance and
express instance of your box? If I understand correctly, the problem only
occurs if you have the default instance and express on the same PC. If you
run your coding on your box, it works fine since you also check local
registry. But if you run your coding on another PC, I think you will get
only the Express instance on your box. I hope I'm wrong.

Tom Shelton said:
Hi Tom,

Thanks for the code and will try it. If I understand your coding correctly,
you're checking local registries for local instances and using GetDataSource
for network instances. My current coding is just using GetDataSource.

The issue that I'm experiencing seems to be the one mentioned in here:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=198078&SiteID=1
I think your coding may not work for this known issue.

Peter



Tom Shelton said:
Additional Info:
If I try to add connection in Server Explorer of VS2008,
- SQL Browser Service: not running
only see MSSQLSERVER
- SQL Browser Service: running
only see mymachine\SQLEXPRESS
So, this behavior is same as GetDataSource or SMO.
I hope someone can explain this strange behavior and tell me how I should
get the list of SQL Server Instances in a VB.Net application.
:
I'm trying to get a list of SQL Server Instances thru a VB.NET application.
I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe
fromhttp://www.sqldev.net/misc/ListSQLSvr.htm
I run all of them on a Windows XP Professional 64bit machine which has 2
instances: mymachine\SQLExpress (Express Edition) and MSSQLSERVER (Developer
Edition (64bit))
SQL Browser Service: not running
- GetDataSource or SMO returns MSSQLSERVER
- ListSQLSvr returns nothing
SQL Browser Service: running
- GetDataSource or SMO or ListSQLSvr returns mymachine\SQLEXPRESS- Hide quoted text -
- Show quoted text -
Here's some C# code I wrote for a dialog box component that mimics the
one from SQL Management Studio... It seems to work for me:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Data;
using System.Data.Sql;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;
namespace FireAnt.Controls.Database
{
[Serializable()]
internal class ServerInstance : IComparable,
IComparable<ServerInstance>, IComparable<string>
{
private string server;
private string instance;
private Version version;
private static readonly string LocalServer = Dns.GetHostName
().ToUpper ();
private const string ServerInstanceRegistryKey = "SOFTWARE\
\MICROSOFT\\MICROSOFT SQL SERVER\\INSTANCE NAMES\\SQL";
#region Constructors
public ServerInstance () : this ( string.Empty ) { }
public ServerInstance ( string server ) : this ( server,
string.Empty ) { }
public ServerInstance ( string server, string instance ) :
this ( server, instance, "0.0" ) { }
public ServerInstance ( string server, string instance, string
version ) : this ( server, instance, new Version ( version ) ) { }
public ServerInstance ( string server, string instance,
Version version )
{
this.server = server;
this.instance = instance;
this.version = version;
}
#endregion
#region Properties
public string Server
{
get { return this.server; }
}
public string Instance
{
get { return this.instance; }
}
public Version Version
{
get { return this.version; }
}
#endregion
#region Overrides
public override string ToString ()
{
StringBuilder displayString = new StringBuilder
( this.Server );
// add the instance name if we have one
if ( !string.IsNullOrEmpty ( this.Instance ) )
{
displayString.AppendFormat ( "\\{0}", this.Instance );
}
// add the version if it is a valid one
if ( this.Version.Major != 0 )
{
displayString.AppendFormat ( " ({0}.{1})",
this.Version.Major, this.Version.Minor );
}
return displayString.ToString ();
}
public override bool Equals ( object obj )
{
if ( obj == null || !( obj is ServerInstance ) )
return false;
return ( this.ToString () == obj.ToString () );
}
public override int GetHashCode ()
{
return this.ToString ().GetHashCode ();
}
#endregion
#region Operators
public static implicit operator string ( ServerInstance
instance )
{
return instance.ToString ();
}
#endregion
public static List<ServerInstance> GetLocalServerList ()
{
List<ServerInstance> localServers = new
List<ServerInstance> ();
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey
( ServerInstanceRegistryKey );
if ( registryKey != null )
{
using ( registryKey )
{
foreach ( string instanceName in
registryKey.GetValueNames () )
{
localServers.Add ( new ServerInstance
( LocalServer, instanceName ) );
}
}
}
return localServers;
}
public static List<ServerInstance> GetNetworkServerList ()
{
List<ServerInstance> networkServers = new
List<ServerInstance> ();
using ( DataTable dataSources =
SqlDataSourceEnumerator.Instance.GetDataSources () )
{
foreach ( DataRow dataSource in dataSources.Rows )
{
networkServers.Add ( new ServerInstance (
(string)dataSource["ServerName"],
Convert.IsDBNull
( dataSource["InstanceName"] ) ? string.Empty :
(string)dataSource["InstanceName"],
Convert.IsDBNull ( dataSource["Version"] ) ?
"0.0" : (string)dataSource["Version"] ) );
}
}
return networkServers;
}
#region IComparable Members
public int CompareTo ( object obj )
{
if ( obj == null || !(obj is ServerInstance ))
return -1;
else
return this.ToString ().CompareTo ( obj.ToString () );


#region IComparable<ServerInstance> Members
public int CompareTo ( ServerInstance other )
{
return this.ToString ().CompareTo ( other.ToString () );
}

#region IComparable<string> Members
public int CompareTo ( string other )
{
return this.ToString ().CompareTo ( other );
}

Anyway, maybe you can make use of this - convert it to vb or
whatever :)

- Show quoted text -

Actually, it seems to work fine here. I have two instances on this
box - the default (it shows up as MSSQLSERVER), and then an named
instance os sql express - SQLEXPRESS.

I also have default on another PC, and it is found also. Basically,
mine matches whats in the management studio dialog - except that in
management studio, the default local instance doesn't show
MSSQLSERVER, mine does.
 
T

Tom Shelton

Hi Tom,

If you run your coding on another PC, does it show the default instance and
express instance of your box? If I understand correctly, the problem only
occurs if you have the default instance and express on the same PC. If you
run your coding on your box, it works fine since you also check local
registry. But if you run your coding on another PC, I think you will get
only the Express instance on your box. I hope I'm wrong.

Hmmm, you may be right - I'll have to try that from the other machine
when I get a minute.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top