2013-04-01 40 views
0

您好我正尝试连接到Windows窗体项目中的本地SQL Server Compact数据库(.sdf),并且一直面临此问题很长一段时间。我不允许为项目使用数据集,所有查询和连接都写在应用程序中。SDF本地数据库连接错误:40

System.Data.SqlClient.SqlException

A network-related or instance-specific error occurredwhile 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)

代码:

SqlConnection _Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString()); 
SqlCommand _Command = _Connection.CreateCommand(); 

_Connection.Open(); // <- throws exception 

回答

2

要连接的SQL Server Compact你需要一组不同的包含在namespace SqlServerCe类(的SqlCeConnection,SqlCeCommand等....)

SqlCeConnection _Connection = new SqlCeConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString()); 
SqlCeCommand _Command = _Connection.CreateCommand(); 
_Connection.Open(); 

当然,您需要引用包含上述类的程序集。

System.Data.SqlServerCe.dll (ADO.NET provider) 

,并添加using语句

using System.Data.SqlServerCe; 
+0

谢谢!这有帮助! – Chris