Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have just started using SMO.

The full name of the MSSQL server on my PC is <machineident>\SQLExpress.

In the project I am working on:

Dim srv As New Microsoft.SqlServer.Management.Smo.Server


When running the code using the above, srv.name only returns the <machineident> portion of the name. It ignores the "\SQLExpress" portion of the name.

As a result of misidentifying the server, subsequent SMO calls do not work correctly. For example:
Dim db As Database = srv.Databases(mDataBaseName)
throws an error.

What I have tried:

1. Searched for reporting of this issue.
2. Looked to the list of methods in .Server object to see if there was a method to correctly set the server.
Posted
Updated 30-Jan-21 5:22am

1 solution

The
\SQLExpress
part of the name is the instance the SQL Server runs on. You will get it with
srv.InstanceName
here ist the microsoft doc for this property (well, not that verbose and not very helpful) perhaps this getting started might help you a bit.

Here is a sample that worked for me

VB
Dim serverName As String = "yourMachine"
Dim instanceName As String = "yourInstance"
Dim srv As New Microsoft.SqlServer.Management.Smo.Server(serverName + "\" + instanceName)
Dim serverNameFromSMO = srv.Name
Dim instanceNameFromSMO = srv.InstanceName
 
Share this answer
 
v2
Comments
Kevin Brady 30-Jan-21 12:51pm    
When I try to get the InstanceName, an error is thrown. "Failed to connect to server"

It seems that SMO is not correctly finding the SQL server.
FranzBe 30-Jan-21 14:40pm    
Hi Kevin, it is difficult so see what your problem is from the one line of code you are posting. I added some lines to the solution that are working on my system. In the getting started link I mentioned there are samples that show you how to connect to a SQL Server (in c# but that might still be helpful). You should use 'Improve Question' and post the relevant part of your code.
Kevin Brady 31-Jan-21 9:59am    
Setting the full name of the dataserver as you detailed works. Thank you very much.
To do this, I had to add a reference to Microsoft.SqlServer.ConnectionInfo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900