150

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?

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 1
    related keywords for server, db, username, password is listed in this answer: http://stackoverflow.com/a/15529085/661933 – nawfal Apr 02 '13 at 20:57

4 Answers4

158

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
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 18
    Begs the question, why has Microsoft created equivalents...? (except to confuse us :-)) – bytedev Oct 19 '18 at 11:23
  • 1
    @bytedev - 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. – Damien_The_Unbeliever Oct 19 '18 at 11:26
  • 6
    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. – Brian Lacy Mar 09 '20 at 23:48
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

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Exel Gamboa
  • 936
  • 1
  • 14
  • 25
13

They are synonymous - you can use either one.

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

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 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? – DonkeyBanana Dec 03 '17 at 12:18
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(.)(.);

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 1
    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. – Click Ok Mar 05 '20 at 23:20
  • @ClickOk Might be, might be... You got it, though, so... :) – Konrad Viltersten Mar 06 '20 at 21:00
  • this makes it easy to copy, paste, grok, filter, and parse connection strings. This is a great answer! – Joshua K Mar 04 '21 at 21:09
  • @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*? – Konrad Viltersten Mar 05 '21 at 13:40
  • @KonradViltersten https://docs.datadoghq.com/logs/processing/parsing/?tab=matcher – Joshua K Mar 05 '21 at 14:17
  • @KonradViltersten "Grok" from book "Stranger in a Strange Land". https://en.wikipedia.org/wiki/Grok – Trevor Aug 23 '22 at 12:35