Asish Pv
Asish Pv

Reputation: 3

Connecting the database without specifying server?

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();
} 

Upvotes: 0

Views: 130

Answers (2)

Nagaraj S
Nagaraj S

Reputation: 13484

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

Upvotes: 0

Ofiris
Ofiris

Reputation: 6151

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 ("")

Upvotes: 4

Related Questions