SqlCe help?

Topics: Data Access Application Block
May 14, 2007 at 6:49 PM
I have trouble connecting the SqlCe database from DAAB, I can connect to it the usual way, via creating a SqlCeConnection, etc. I've setup the custome provider in the App.Config file according to the help file with the connection string pointing to my SqlCe database file. My code is:

Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand("select * from table1");
DataSet ds = new DataSet();
db.LoadDataSet(cmd, ds, "table1");

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds.Tables0;

and it will give me an error of "Could not load file or assembly Microsoft.Practices.EnterpriseLibrary.Data". This is how I always use it to access Sql Server 2005 or Express edition, do I need to do something different for sqlce here?

if I added the following line at the begining it will work fine but I don't want to define the connection string here since I did already in app.config:

SqlCeDatabase db = new SqlCeDatabase(@"Data Source=D:\Data\ce\dbfile.sdf");


May 15, 2007 at 4:25 PM
I mentioned this quite some time ago in the issue tracker:


and just realized nobody ever commented on it. I assume it was never resolved.

The solution is to add a ProviderMapping that you can do graphically from the Config Tool:

        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
            <add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.SqlCe.SqlCeDatabase, Microsoft.Practices.EnterpriseLibrary.Data.SqlCe, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                name="System.Data.SqlServerCe" />

A call to DatabaseFactory.CreateDatabase() should use the SqlCeDatabase Class.

However, you likely my want to use the SqlCeDatabase Class directly anyway since it has so many useful methods not in the Abstract Database Class.




David Hayden
Microsoft MVP C#