简体   繁体   English

无法从C#连接到SQL Server Compact

[英]Cannot connect to SQL Server Compact from C#

I am brand new to Visual C#. 我是Visual C#的新手。 (I come from javaland) (我来自javaland)

I used the IDE's Data -> Add New Data Source tool to create a database my app can connect to. 我使用IDE的Data -> Add New Data Source工具来创建我的应用程序可以连接的数据库。 I can open the database from the IDE's Database Explorer, but I can't connect to the database from the app. 我可以从IDE的数据库资源管理器中打开数据库,但我无法从应用程序连接到数据库。

The database is a SQL Server Compact 3.5 file with no password or encryption. 该数据库是SQL Server Compact 3.5文件,没有密码或加密。 This is the auto generated connection string: 这是自动生成的连接字符串:

Data Source=|DataDirectory|\test.sdf

but when I run the following code ... 但是当我运行以下代码时......

  SqlConnection con = new SqlConnection("Data Source=|DataDirectory|\\test.sdf");
  con.Open();

I get the error 我收到了错误

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)

at con.Open() ; con.Open() ;

What am I doing wrong? 我究竟做错了什么?

Thanks for your help! 谢谢你的帮助!

You should be using the SqlCeConnection class, not SqlConnection . 您应该使用SqlCeConnection类,而不是SqlConnection You may have to add a reference to the system.data.sqlserverce assembly. 您可能必须添加对system.data.sqlserverce程序集的引用。 The SqlConnection class is used to connect to a full version of SQL Server (including Express). SqlConnection类用于连接到完整版本的SQL Server(包括Express)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM