0

Database=xxx;User ID=xxx;Password=xxx;

Why this connection string works without specifying server /data source. This connected MS SQL SERVER (running in local) from windows app

private static string connectionstr = "Database=xxx; User ID=xxx; Password=xxx;"; 
public void connect()
{ 
    scon = new SqlConnection(connectionstr);
    scon.Open();
} 
4
  • 3
    Can you show how do you use it in your code ? Commented Jan 28, 2014 at 12:53
  • private static string connectionstr = "Database=xxx;User ID=xxx;Password=xxx;"; public void connect(){ scon = new SqlConnection(connectionstr);scon.Open();} Commented Jan 28, 2014 at 12:56
  • it works fine and connected the database without give server/data source, the sql server in local.why? Commented Jan 28, 2014 at 12:57
  • See my answer, note its a better to explicitly declare server / datasource as on different platforms you might have issues related to loop-back and more. Commented Jan 28, 2014 at 13:10

2 Answers 2

4

Creating SqlConnection with a connection string which has no DataSource results in a default value which is an empty string ("").

empty string ("") will be translated later to localhost, try ping "" from cmd and see you actually pinging localhost.

See MSDN SqlConnection, Remarks section - DataSource initial value is empty string ("")

Sign up to request clarification or add additional context in comments.

Comments

0

The default host name is your localhost.So it will connect your localhost without specifying servername MSDN

1 Comment

@AsishPv If my answer is useful make upvote or mark as useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.