Connecting C# to SQL Server Compact database

房东的猫 提交于 2019-12-01 19:25:59

Try use SqlCeConnection class rather than SqlConnection:

SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf;Persist Security Info=False";

If you want to connect to SQL Server Compact, use SqlCeConnection, SqlCeCommand etc. Add a reference to the SQL Server Compact ADO.NET provider, System.Data.SqlServerCe.dll

Have a look at this blog article: SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

This goes step-by-step through what you might need to do:

In short:

  1. SQL Server should be up and running.
  2. Enable TCP/IP in SQL Server Configuration
  3. Open Port in Windows Firewall
  4. Enable Remote Connection
  5. Enable SQL Server Browser Service
  6. Create exception of sqlbrowser.exe in Firewall
  7. Recreate Alias

About the where and what to do in each step, you will find more in-depth information in the article.

You may also want to have a look at the Connection strings for SQL Server Compact. There you can find other variations of the connection string you could try to play with.

You seems to be using a bad connection string. (Or the file path is wrong). Check out http://www.connectionstrings.com/sql-server-ce for connection string options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!