Going nuts with SQL Server Compact Edition

by Codewiz51 February 20, 2010 16:22

I've been experimenting with converting a small Oracle database to SQL Server Compact Edition 3.5.  While I'm nonplussed with the available tools, in particular the lack of support for SQLCE in DTSWizard, the database is highly function for embedded purposes.

One thing that has been driving me nuts is a piped variable that VS 2008 places in the Settings variable for the path: DataDirectory. Your predefined connection string contains a term for Data Source that looks something like |DataDirectory|\EmbeddedAppDB.sdf.  The .Net runtime expands the term |DataDirectory| as follows:

  1. For applications placed in a directory on the user machine, this will be the app's (.exe) folder. (This means you will have to place your sdf file in the Release or Debug folders.)
  2. For apps running under ClickOnce, this will be a special data folder created by ClickOnce.
  3. For Web apps, this will be the App_Data folder.

I've found that you can place code similar to the following in your form load and class constructor to point the DataDirectory to the desired location:

// Declare a string and assign a database path.

string s = @"C:\MyDataDirectory";

// Assign the property to the current domain for the thread

AppDomain.CurrentDomain.SetData("DataDirectory", s);

// Now you can run Debug or Release without copying the DB.

this.contactsTableAdapter.Fill(this.embeddedAppDB.Contacts);

Using this code, you can place your database in a known location and use it without copying to each executable directory used by the IDE.

Note:

Generally, the following call to GetData returns null unless you set the value of DataDirectory:

// Assign the property value to a string.  Usually returns null
// unless you explicity set the value of Data Directory

string s = (string)AppDomain.CurrentDomain.GetData("DataDirectory");

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011