无法从Visual Studio连接到SQL Server 2008 R2

无法从Visual Studio连接到SQL Server 2008 R2

问题描述:

我在win 7 32bit中安装了Sql Server 2008 R2.它已正确安装并建立了数据库.我打开了Visual Studio 2010,并编写了一个连接到Sql Server的代码.

I installed Sql Server 2008 R2 in win 7 32bit. It installed correctly and made a database. I opened visual studio 2010 and wrote a code that it connects to Sql server.

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



但出现此错误.



but it has this error.

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)



Visual Studio自动安装了SQL Express.我禁用了Internet安全性.



Visual Studio has installed SQL Express automatically.I disable my Internet security.

您遇到以下两种情况之一:
1)您的连接字符串是kaput,需要固定(这是我的假设,因为(local)"不太可能是有效的实例名称.此值需要反映使用Management Studio登录时使用的正确实例名称) .通常类似于"localhost/INSTANCE"或"MACHINENAME/INSTANCE".
2)您的SQL Server不接受远程连接.
You have either one of two situations:
1) Your connection string is kaput and needs to be fixed (This is my assumption, as "(local)" is not likely a valid instance name. This value needs to reflect the proper instance name that you use when you logon using Management Studio. Usually something like ''localhost/INSTANCE'', or ''MACHINENAME/INSTANCE''.
2) Your SQL server is not accepting remote connections.