BSen
BSen

Reputation: 187

Get instance name from data source name

I'm using SQLDriverConnect function to connect to database. In connection string I can specify ODBC pre-configured data source name (DSN), function resolves necessary attributes and all works fine. But after successful connection I need to get instance name to which I have connected or connection port (because there can be several instances of mssql running on server). How can I implement this?

Upvotes: 0

Views: 1453

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239636

Run the following query on your connection:

select @@SERVERNAME

This will return the server and instance name

The preferred form is apparently to use SERVERPROPERY:

SELECT SERVERPROPERTY('ServerName')

which will return the server and instance name, and, unlike @@SERVERNAME, correctly returns results if the server has been renamed.

Upvotes: 1

Related Questions