简体   繁体   中英

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?

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);
}
  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

Open SQL Server Configuration Manager: 在此处输入图像描述

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]);
  }     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM