"Server" vs "Data Source" in connection string
Asked Answered
A

4

157

I'm new to SqlServer, right now I have SqlLocalDb installed to work locally. Good, but I can see two connection strings typically and both works:

Data Source=(localdb)\v11.0;Integrated Security=true;

and

Server=(localdb)\v11.0;Integrated Security=true;

What exact difference is there between the two?

Alvin answered 22/2, 2013 at 13:11 Comment(1)
related keywords for server, db, username, password is listed in this answer: https://mcmap.net/q/77044/-is-there-any-connection-string-parser-in-cAlvin
D
162

For the full list of all of the connection string keywords, including those that are entirely synonymous, please refer to the SqlConnection.ConnectionString documentation:

These are all entirely equivalent:

  • Data Source
  • Server
  • Address
  • Addr
  • Network Address
Dolhenty answered 22/2, 2013 at 13:48 Comment(3)
Begs the question, why has Microsoft created equivalents...? (except to confuse us :-))Sacred
@Sacred - historical confluence, I believe. Most of these names started out being used in other, older DB connection "standards". When building ADO.Net, so long as there aren't conflicting usages, you may as well allow as many common ones as exist in older standards, to ease porting code.Dolhenty
It may be useful to know that, if for some reason your connection string includes more than one of these keywords (and the address values conflict), the last item is used; the previous values are ignored. So for example, given the connection string, Server=192.168.2.2;Data Source=localhost, the client will honor the localhost value and ignore the 192... value.Sacrifice
G
20

... There is no difference between Server and Data Source as they represent the same thing for SQL Server : the full name of the SQL Server instance with the syntax "MyComputerName\MyShortInstanceName" , potentially including the port used by the SQL Server instance to communicate.

Reference: http://social.msdn.microsoft.com/Forums/en/sqldataaccess/thread/7e3cd9b2-4eed-4103-a07a-5ca2cd33bd21

Gasworks answered 22/2, 2013 at 13:23 Comment(0)
G
13

They are synonymous - you can use either one.

That is - as far as the framework is concerned, they are the same.

Gudrunguelderrose answered 22/2, 2013 at 13:12 Comment(1)
I've been googlearching for the reason for the range of equivalent keywords in the connection strings. This far, I haven't found a good explanation. I'm assuming it's due to historial reasons and users from different "worlds" coming together. Is there another reason?Calender
B
9

My favorite set up is one that doesn't contain any spaces. In the simplest form, one has to provide four values - the URL, the container, the user and the credential.

  • server
  • database
  • user (or uid)
  • password (or pwd)

So a connection string looks like this.

server=stuffy.databases.net;database=stuffy;user=konrad;password=Abc123(.)(.);

Brathwaite answered 23/6, 2019 at 19:56 Comment(6)
Konrad, I think downvoters did not understand what you said. You mean, by example, its better "server" than "data source" because one word does not contains spaces. The same for "uid" instead of "user id". I think your answer is valid.Collimore
@ClickOk Might be, might be... You got it, though, so... :)Brathwaite
this makes it easy to copy, paste, grok, filter, and parse connection strings. This is a great answer!Bareback
@JoshuaK Thanks for the kind remark. I agree with every word except grok. Because I don't know what it means... How does one grok a string?Brathwaite
@KonradViltersten docs.datadoghq.com/logs/processing/parsing/?tab=matcherBareback
@KonradViltersten "Grok" from book "Stranger in a Strange Land". en.wikipedia.org/wiki/GrokUnlisted

© 2022 - 2024 — McMap. All rights reserved.