Home
%3CLINGO-SUB%20id%3D%22lingo-sub-383478%22%20slang%3D%22en-US%22%3EUnderstanding%20%E2%80%9CData%20Source%3D(local)%E2%80%9D%20in%20SQL%20Server%20Connection%20Strings%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383478%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2019%2C%202008%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ELately%20we%20have%20noticed%20many%20misunderstandings%20surrounding%20the%20usage%20of%20the%20Data%20Source%20keyword%20in%20connection%20strings%2C%20caused%20by%20people%20generalizing%20from%20an%20example%20demonstrating%20a%20connection%20string%20for%20local%20connectivity%20and%20creating%20their%20own%20connection%20string%20for%20a%20remote%20connection.%26nbsp%3B%20Here%20is%20one%20such%20example%20connection%20string%20for%20local%20connectivity%20as%20it%20would%20be%20used%20in%20VB.Net%3A%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EPublic%20con%20As%20New%20System.Data.SqlClient.SqlConnection(%22Integrated%20Security%3DSSPI%3BInitial%20Catalog%3DTestDatabase%3BData%20Source%3D(local)%3B%22)%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EThis%20connection%20string's%20options%20can%20be%20dissected%20as%3A%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E-%20%3CB%3E%20Integrated%20Security%3DSSPI%3B%20%3C%2FB%3E%20%E2%80%93%20This%20means%20we%20want%20to%20connect%20using%20Windows%20authentication%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E-%20%3CB%3E%20Initial%20Catalog%3DTestDatabase%3B%20%3C%2FB%3E%20%E2%80%93%20This%20means%20the%20database%20we%20want%20to%20first%20connect%20to%20is%20named%20%E2%80%9CTestDatabase%E2%80%9D.%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E-%20%3CB%3E%20Data%20Source%3D(local)%3B%20%3C%2FB%3E%20%E2%80%93%20This%20means%20that%20we%20want%20to%20connect%20to%20the%20SQL%20Server%20instance%20located%20on%20the%20local%20machine.%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20confusion%20occurs%20with%20this%20last%20option%2C%20since%20people%20think%20that%20%E2%80%9Clocal%E2%80%9D%20is%20a%20keyword%20referring%20to%20the%20local%20machine%20account%2C%20when%20in%20fact%20the%20special%20keyword%20is%20%22(local)%22%2C%20including%20the%20parentheses.%26nbsp%3B%20As%20a%20result%2C%20if%20they%20want%20to%20use%20a%20remote%20connection%2C%20and%20if%20their%20server%E2%80%99s%20name%20is%2C%20for%20example%2C%20TestServer%20with%20IP%20address%2010.1.1.10%2C%20they%20try%20to%20use%20the%20connection%20string%20option%3A%20%E2%80%9CData%20Source%3D(TestServer)%E2%80%9D%20or%20%E2%80%9CData%20Source%3D(10.1.1.10)%E2%80%9D.%26nbsp%3B%20Since%20the%20special%20keyword%20here%20is%20the%20whole%20word%20%E2%80%9C(local)%E2%80%9D%2C%20the%20correct%20connection%20string%20option%20would%20be%3A%20%E2%80%9C%20%3CB%3E%20Data%20Source%3DTestServer%20%3C%2FB%3E%20%E2%80%9D%20or%20%E2%80%9C%20%3CB%3E%20Data%20Source%3D10.1.1.10%20%3C%2FB%3E%20%E2%80%9D.%26nbsp%3B%20So%2C%20as%20a%20whole%20line%20of%20code%2C%20this%20would%20now%20read%3A%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EPublic%20con%20As%20New%20System.Data.SqlClient.SqlConnection(%22Integrated%20Security%3DSSPI%3BInitial%20Catalog%3DTestDatabase%3BData%20Source%3DTestServer%22)%3C%2FP%3E%0A%20%20%3CP%3Eor%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EPublic%20con%20As%20New%20System.Data.SqlClient.SqlConnection(%22Integrated%20Security%3DSSPI%3BInitial%20Catalog%3DTestDatabase%3BData%20Source%3D10.1.1.10%22)%3C%2FP%3E%0A%20%20%3CP%3EDan%20Benediktson%20%3CBR%20%2F%3E%20SQL%20Server%20Protocols%20%3CBR%20%2F%3E%20Disclaimer%3A%20This%20posting%20is%20provided%20%22AS%20IS%22%20with%20no%20warranties%2C%20and%20confers%20no%20rights%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383478%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2019%2C%202008%20Lately%20we%20have%20noticed%20many%20misunderstandings%20surrounding%20the%20usage%20of%20the%20Data%20Source%20keyword%20in%20connection%20strings%2C%20caused%20by%20people%20generalizing%20from%20an%20example%20demonstrating%20a%20connection%20string%20for%20local%20connectivity%20and%20creating%20their%20own%20connection%20string%20for%20a%20remote%20connection.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383478%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerProtocols%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Sep 19, 2008

Lately we have noticed many misunderstandings surrounding the usage of the Data Source keyword in connection strings, caused by people generalizing from an example demonstrating a connection string for local connectivity and creating their own connection string for a remote connection.  Here is one such example connection string for local connectivity as it would be used in VB.Net:

Public con As New System.Data.SqlClient.SqlConnection("Integrated Security=SSPI;Initial Catalog=TestDatabase;Data Source=(local);")

This connection string's options can be dissected as:

- Integrated Security=SSPI; – This means we want to connect using Windows authentication

- Initial Catalog=TestDatabase; – This means the database we want to first connect to is named “TestDatabase”.

- Data Source=(local); – This means that we want to connect to the SQL Server instance located on the local machine.

The confusion occurs with this last option, since people think that “local” is a keyword referring to the local machine account, when in fact the special keyword is "(local)", including the parentheses.  As a result, if they want to use a remote connection, and if their server’s name is, for example, TestServer with IP address 10.1.1.10, they try to use the connection string option: “Data Source=(TestServer)” or “Data Source=(10.1.1.10)”.  Since the special keyword here is the whole word “(local)”, the correct connection string option would be: “ Data Source=TestServer ” or “ Data Source=10.1.1.10 ”.  So, as a whole line of code, this would now read:

Public con As New System.Data.SqlClient.SqlConnection("Integrated Security=SSPI;Initial Catalog=TestDatabase;Data Source=TestServer")

or:


Public con As New System.Data.SqlClient.SqlConnection("Integrated Security=SSPI;Initial Catalog=TestDatabase;Data Source=10.1.1.10")

Dan Benediktson
SQL Server Protocols
Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights