humcfc
humcfc

Reputation: 349

Get instance name of local sql server

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

Answers (4)

David
David

Reputation: 107

Open SQL Server Configuration Manager: enter image description here

Upvotes: 0

Yasir Siddiqui
Yasir Siddiqui

Reputation: 11

  1. Search for your computer name in cmd by typing "hostname".
  2. In Run Type "Services.msc", to access the windows services.
  3. Search in services that a service is running with "sql server (...)","sql server agent (...)" and " sql server browser (...)" in your services, if they are not running , then right click on each of them and start them.
  4. In Services list,check for the name written in bracket of "sql server (...)" for ex. "SQL Server (XYZ)", so the name of instance is "XYZ"
  5. Go to sql server configuration manager > sql server network configuration > Protocols for .. >TCP/IP (make it enabled)
  6. After this now again go to your services and restart the services of sql server.
  7. Now if your computer name is "ABC" and instance is "XYZ", try connecting with "ABC\XYZ" using windows authentication [if you have not configured sql authentication]
  8. Should you have any issue then please post back, I will give a more detailed version of it

Upvotes: 0

Raj Ranjhan
Raj Ranjhan

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

IUnknown
IUnknown

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

Related Questions