1

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?

Cois
  • 83
  • 7

3 Answers3

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
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