469,292 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

How to find the SQL Server 2005 port

I am using a System.Data.SqlClient.SqlConnection to connect to a SQL
Server 2005 database. I instantiate the SqlConnection with a
connection string...

SqlConnection sqlConn = new SqlConnection(@"Network
Library=DBMSSOCN;Data Source=(local)" +
",1433;Database=CATHYDB;User
id=cathy;Password=pswd;");

When this code runs I can guarantee that everything about it will be
constant, except the port (here I've coded it as 1433).

I want to avoid hard-coding the port number. So, how can I either

a) find out which port SQL Server 2005 is using, or
b) use a different approach which doesn't require a hard-coded port
number?

Many thanks,

Cathy

Jun 12 '07 #1
9 25821
Hi Cathy,

There are 65,536 TCP ports on any machine. So, you can't exactly "discover"
the port, and that's not generally done. It is always expected that the
client will know what port to look at.

If you don't want to hard code the port number into your Connection String,
host the SQL Server on its' default port.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Cathy" <ca**********@yahoo.co.ukwrote in message
news:11*********************@q19g2000prn.googlegro ups.com...
>I am using a System.Data.SqlClient.SqlConnection to connect to a SQL
Server 2005 database. I instantiate the SqlConnection with a
connection string...

SqlConnection sqlConn = new SqlConnection(@"Network
Library=DBMSSOCN;Data Source=(local)" +
",1433;Database=CATHYDB;User
id=cathy;Password=pswd;");

When this code runs I can guarantee that everything about it will be
constant, except the port (here I've coded it as 1433).

I want to avoid hard-coding the port number. So, how can I either

a) find out which port SQL Server 2005 is using, or
b) use a different approach which doesn't require a hard-coded port
number?

Many thanks,

Cathy

Jun 12 '07 #2

"Cathy" <ca**********@yahoo.co.ukwrote in message
news:11*********************@q19g2000prn.googlegro ups.com...
>I am using a System.Data.SqlClient.SqlConnection to connect to a SQL
Server 2005 database. I instantiate the SqlConnection with a
connection string...

SqlConnection sqlConn = new SqlConnection(@"Network
Library=DBMSSOCN;Data Source=(local)" +
",1433;Database=CATHYDB;User
id=cathy;Password=pswd;");

When this code runs I can guarantee that everything about it will be
constant, except the port (here I've coded it as 1433).

I want to avoid hard-coding the port number. So, how can I either

a) find out which port SQL Server 2005 is using, or
b) use a different approach which doesn't require a hard-coded port
number?

SQL Server no matter what version it is from 6.0 to 2005 uses ports 1433 and
1434. Those are the standards. SQL Server is not going to be listening on
any other ports.

And why are you hard coding the port number? The only database solution I
have seen where the port number had to be give was Oracel using NHibernate.


Jun 12 '07 #3
Sorry to be the bearer, but you can specify any port you like (within
reason) via the network configuration tool / server network utility.
1433 is just the default (although a very common default [i.e.
probably 99%+])

Marc
Jun 12 '07 #4
On Jun 12, 12:49 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:

<snip>
SQL Server no matter what version it is from 6.0 to 2005 uses ports 1433 and
1434. Those are the standards. SQL Server is not going to be listening on
any other ports.
Well, those are the defaults - for the default instance. It can be
changed even for the default instance, and I *think* each instance
will have its own port, so if you've got more than one instance then
at least *one* of them will be listening on a port other than the ones
you listed.

Jon

Jun 12 '07 #5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
On Jun 12, 12:49 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:

<snip>
>SQL Server no matter what version it is from 6.0 to 2005 uses ports 1433
and
1434. Those are the standards. SQL Server is not going to be listening on
any other ports.

Well, those are the defaults - for the default instance. It can be
changed even for the default instance, and I *think* each instance
will have its own port, so if you've got more than one instance then
at least *one* of them will be listening on a port other than the ones
you listed.
But I guarantee that 99.9% of SQL Servers are not listing on any other ports
and are using the standard ports. If different ports were being used, even
if I was pointing to another instance, that would be a know factor, and I
wouldn't be trying to come up with something to find SQL Server on high
ports. :)
Jun 12 '07 #6
On Jun 12, 1:38 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
Well, those are the defaults - for the default instance. It can be
changed even for the default instance, and I *think* each instance
will have its own port, so if you've got more than one instance then
at least *one* of them will be listening on a port other than the ones
you listed.

But I guarantee that 99.9% of SQL Servers are not listing on any other ports
and are using the standard ports.
Agreed. That's not the same as saying that SQL Server is not going to
be listening on any other ports though.

Jon

Jun 12 '07 #7
True.
Every instance uses a different TCP port. Named instances use, by default,
dynamic ports, and will change on every restart.
A named instance can be bound to a specific port but that's not necessary
with SQL 2005, because of SQL Browser service.
SQL Browser implements a lookup service via UDP 1434 (that will not change
easily) that the client uses to find out the port the named instance is
using.

http://msdn2.microsoft.com/en-us/lib...7(SQL.90).aspx
"Jon Skeet [C# MVP]" <sk***@pobox.comha scritto nel messaggio
news:11**********************@z28g2000prd.googlegr oups.com...
On Jun 12, 12:49 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:

<snip>
>SQL Server no matter what version it is from 6.0 to 2005 uses ports 1433
and
1434. Those are the standards. SQL Server is not going to be listening on
any other ports.

Well, those are the defaults - for the default instance. It can be
changed even for the default instance, and I *think* each instance
will have its own port, so if you've got more than one instance then
at least *one* of them will be listening on a port other than the ones
you listed.

Jon
Jun 12 '07 #8
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
Agreed. That's not the same as saying that SQL Server is not going to
be listening on any other ports though.
Quite so - one of my current clients has all of its SQL Server instances
running on ports above 50,000...

It's the usual story - somebody somewhere read something which said that
running SQL Server on the default port (even on internal networks) was a
massive security risk, so they went into panic mode and changed them all...
:-)
--
http://www.markrae.net

Jun 12 '07 #9

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
On Jun 12, 1:38 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:
Well, those are the defaults - for the default instance. It can be
changed even for the default instance, and I *think* each instance
will have its own port, so if you've got more than one instance then
at least *one* of them will be listening on a port other than the ones
you listed.

But I guarantee that 99.9% of SQL Servers are not listing on any other
ports
and are using the standard ports.

Agreed. That's not the same as saying that SQL Server is not going to
be listening on any other ports though.
You can change a Web server not ot listen on TCP 80 as well. But since I am
not a DBA and would have no reason to know that SQL can run on other ports,
because that's all I have seen is the standards for connecting to SQL Server
in the many shops I have worked in, then so be it, as I have actually
learned something new about SQL Server.

And I'll also say that a vast majority of so called SQL Server Admins don't
know it either. They would just put up another SQL Server box.

Jun 12 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Danieltbt05 | last post: by
4 posts views Thread by Ryan | last post: by
10 posts views Thread by MVChauhan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.