[英]SDF Local database Connection error: 40
您好我正在尝试连接到Windows窗体项目中的本地SQL Server Compact数据库( .sdf
)并且已经面对这个问题很长一段时间了。 我不允许为项目使用数据集,所有查询和连接都写在应用程序中。
System.Data.SqlClient.SqlException
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)
码:
SqlConnection _Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString());
SqlCommand _Command = _Connection.CreateCommand();
_Connection.Open(); // <- throws exception
要连接Sql Server Compact,您需要在命名空间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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.