Connecting to SQL CE db using SQLConnection

China☆狼群 提交于 2019-12-04 03:16:24

It's actually very possible to user SQL CE instead of full-blown SQL Server by only modifying configuration parameters: change connection string and use IDbXXX family interfaces wherever possible instead of platform-specific SqlXXX and SqlCeXXX ones. See DbProviderFactories.

Be advised, however, of differences in SQL dialects of these two platforms.

Yes you can use SQL Compact and/or SQL Server by referring to the base classes instead. For example:

IDbConnection Connection;

if (Compact)
    Connection = new SqlCeConnection();
else
    Connection = new SqlConnection();

The connection string needs to point at your data file for SQL Compact, for example: "Data Source=urData.sdf;Persist Security Info=False;". More examples here.

This link will explain what differences there are between SQL Server and SQL Compact, as it's not identical.

All of the SQL related objects you need for either DB inherit from the base abstract Db... (ie DbConnection, DbDataAdapter etc...). You can therefore write some kind of DatabaseManager class that when instantiated needs to know if you're dealing with Sql or Sql Ce. Then, from that point forward you deal just with the base class objects (DbConnection etc..). That way, all you need to change every time, is that instantiation of your manager class.

Another plus to doing it this way, is if you later decide to switch to another provider altogether, not much code needs to change.

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