Reputation: 349
I'm totally confused of how to get the local sql server instance. The only thing I receive is the Server names. Here are the different approaches I have tried:
DataTable availableServers = SmoApplication.EnumAvailableSqlServers([All of the 3 inputs]);
foreach (DataRow serverRow in availableServers.Rows)
{
// foreach row list "Name", "Server", "Instance" etc...
}
and
SqlDataSourceEnumerator sdse = SqlDataSourceEnumerator.Instance;
DataTable table = sdse.GetDataSources();
foreach (DataRow row in table.Rows)
{
// foreach row list "ServerName", "InstanceName", "IsClustered" and "Version"
}
and
ManagedComputer mc = new ManagedComputer();
ServerInstance instance = mc.ServerInstances;
Is there some other way I can get the local sql server instance name?
I know that when connecting to SQL server in management studio the connect dialog displays my local instance as .\sqlexpress or (local)\sqlexpress. Also when deploying a database installation in InstallShield the InstallShield "locate sqlserver"-dialog gets the correct instance for my local host. What are they doing correct?
Upvotes: 2
Views: 15036
Reputation: 11
Upvotes: 0
Reputation: 3917
You can use ManagedComputer
using Microsoft.SqlServer.Management.Smo.Wmi;
....
ManagedComputer mc = new ManagedComputer();
foreach (ServerInstance si in mc.ServerInstances)
{
Console.WriteLine("The installed instance name is " + si.Name);
}
Upvotes: 2
Reputation: 898
You have already reached the halfway stage with the second code sample you posted. The DataTable returned will have 4 columns (Collection of columns)
ServerName InstanceName IsClustered Version
You can enumerate over this collection to get the values in the DataTable, like this.
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
}
}
Upvotes: 0