2

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?

4 Answers 4

2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That was the third try I displayed in my question. The Name is empty.
It might be a configuration issue. I assume that the instance is on the same machine? Have tried setting ProviderArchitecture? mc.ConnectionSettings.ProviderArchitecture = ProviderArchitecture.XXX
Thank you very much! Had to set ProviderArchitecture.Use64bit
0

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

4 Comments

I left those lines because only wanted to show which techniques I used. The problem is somehow that the InstanceName is empty.
Check your firewall and SQL Server Browser service. Refer this and this
Can't modify the firewall the way I want. Solved it with ManagedComputer instead. Thanks for your reply anyway.
Great, care to post the approach in your OP?
0
  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

Comments

0

Open SQL Server Configuration Manager: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.