0

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?

2
  • Did you look at the OutConnectionString SQLDriverConnect returns? It might contain what you need. Commented Oct 29, 2012 at 10:12
  • SQLGetInfo + SQL_SERVER_NAME ? Commented Oct 29, 2012 at 11:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I will consider this, but aren't there any way to do this without making any queries?

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.