c# - SQL Server 精简版 4.0 : Error: 26 - Error Locating Server/Instance Specified

标签 c# sql-server-ce

我一直在开发控制台应用程序 (C#/.Net Framework 4.0/VS2012)。我创建了一个 SQL Server Compact 数据库 (*.sdf) 并添加了一个连接字符串:

<connectionStrings>
    <add name="Dispatcher.Properties.Settings.FakeDataSetConnectionString"
        connectionString="Data Source=|DataDirectory|\FakeDataSet.sdf"
        providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>

但是当我尝试执行以下代码时:

SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Dispatcher.Properties.Settings.FakeDataSetConnectionString"].ToString();
con.Open();

它在 con.Open() 中给出以下异常:

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

  1. 我做错了什么?

  2. SQL Server Agent 和 SQL Server Browser 都处于“已启动”状态。使用 SQL Server Compact Edition 真的很重要吗?

最佳答案

对于 SQL Server Compact,您需要使用 SqlCeConnection(不是 SqlConnection - 这是为了“真正的” "SQL Server 版本)。

SqlCeConnection con = new SqlCeConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Dispatcher.Properties.Settings.FakeDataSetConnectionString"].ToString();
con.Open();

当然,您还需要使用 SqlCeCommand(不是 SqlCommand)等等...

参见 MSDN SQL Server Books Online documentation有关 System.Data.SqlServerCe 命名空间

中所有类的更多详细信息

关于c# - SQL Server 精简版 4.0 : Error: 26 - Error Locating Server/Instance Specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874746/

相关文章:

C# system.io.filenotfoundexception 异常

c# - SQL Server CE阅读器问题,不想读了!

c# - 将远程 SQL Server 数据库与本地 SQL Server Compact 数据库同步的最佳方法?

c# - 从 ssl 上的浏览​​器访问在本地主机上运行的 SignalR 应用程序

c# - 这可以通过单个 LINQ 查询实现吗?

c# - Type.GetInterfaces() 仅用于声明的接口(interface)

c# - SQL Compact Edition 3.5 - 不允许访问数据库文件

c# - 将相似的方法浓缩成一个 super 酷的方法

c# - 无法从 'string' 转换为 'char[]' 以进行拆分

c# - 我可以在我的开发机器上为 C# VS2010 项目使用哪种轻型 SQL Server 类型?