开发者

Cannot connect to SQL Server Compact from C#

I am brand new to Visual C#. (I come from javaland)

I used the IDE's Data -> Add New Data Source tool to create a database my app can connect to. I can open the database from the IDE's Database Explorer, but I can't connect to the database from the app.

The database is a SQL Server Compact 3.5 f开发者_StackOverflowile with no password or encryption. 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. 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)

at con.Open();

What am I doing wrong?

Thanks for your help!


You should be using the SqlCeConnection class, not SqlConnection. You may have to add a reference to the system.data.sqlserverce assembly. The SqlConnection class is used to connect to a full version of SQL Server (including Express).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜