SQLException未处理在建立与SQL Server的连接时发生网络相关或实例特定的错误

问题描述:

我在win7 32位中安装了SQL Server 2008 R2和Visual Studio 2010。他们安装正确。而且每个人都可以独自行事但是当我想从Visual Studio连接到SQL Server Management Studio时,出现此错误。 (我想连接到本地数据库):SQLException未处理在建立与SQL Server的连接时发生网络相关或实例特定的错误

SQLException was unhandled A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL server is configured to allow remote connections.(provider:Named Pipes Provider,error:40-could not open a connection to SQL Server)

这是我连接代码:

cn = new SqlConnection(); 
cn.ConnectionString = "Data Source=(local); Initial Catalog=XXX_amlak; User ID=sa; password=XXX"; 
cn.Open(); 
+0

安装了哪个版本的SQL Server。快递还是标准? –

+0

SQL Server 2008 R2企业版 –

使用此的ConnectionString连接到本地数据库:

cn.ConnectionString = "Data Source=.\SqlExpress; Initial Catalog=XXX_amlak; User ID=sa; password=XXX";

或检查出这个问题: How to connect to local instance of SQL Server 2008 Express