连接数据库而无需指定服务器



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

为什么此连接字符串在没有指定服务器/数据源的情况下工作。此连接的MS SQL Server(在本地运行)从Windows App

private static string connectionstr = "Database=xxx; User ID=xxx; Password=xxx;"; 
public void connect()
{ 
    scon = new SqlConnection(connectionstr);
    scon.Open();
} 

使用没有DataSource的连接字符串创建SqlConnection会导致默认值,该值是一个空字符串(")。

空字符串(")将在稍后翻译为localhost,从cmd尝试ping "",然后看到您实际pinging localhost

参见MSDN SQLConnection,备注部分-DataSource初始值是empty string ("")

默认主机名是您的localhost。

最新更新