In some part of my code, I have a SqlConnection object that is open. How do I find out the port of the Sql Server Instance it is connected to?
Asked
Active
Viewed 1,253 times
3 Answers
1
you can query it;
USE master
GO
xp_readerrorlog 0, 1, N'Server is listening on'
GO

Decoded
- 159
- 1
- 10
0
By default it will be connected to TCP port 1433
. Well not sure if there is any other better way present but you can get this information from registry entry key TcpPort
from below registry path
HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSSQLSERVER\MSSQLSERVER\SUPERSOCKETNETLIB\TCP
You can use Registry.GetValue()
method to read this information from system registry.

Rahul
- 76,197
- 13
- 71
- 125
-
Have you tried checking the `ConnectionString` property? – dmeglio Sep 05 '15 at 00:12
0
It will be port 1433 unless you have specified a different port in the connection string.
For information on connection string options see: How to specify a port number in SQL Server connection string? and http://www.connectionstrings.com/

Community
- 1
- 1

StillLearnin
- 1,391
- 15
- 41