Proof of concept: C#-SQLite running in Silverlight

Noah Hart has ported SQLite to C#. I was intrigued to see whether the code could be adapted to run in Silverlight, which has a cut-down .NET Framework and prohibits platform invoke to native code.

I hacked away at his code until it ran in Silverlight:

Note that the sole purpose of the exercise was to see if SQLite could be made to run, not to prepare a port that is production-ready or even code that is fit to check-in. In essence, I defined SQLITE_SILVERLIGHT, removed all the locking, adapted the file I/O to use isolated storage, and generally took as many shortcuts as I could think of to get it to compile and run. Unfortunately transactions do not work, though that can probably be fixed.

As far as I can tell, Silverlight’s Isolated Storage does not support locking at all. If the same application were opened twice, for example in different browsers, that could cause problems, but in normal use it would likely be OK.

One other point of interest: the XAP file in release mode is 257KB.

Update: The sample app is here. The hacked code is here [zip].

13 thoughts on “Proof of concept: C#-SQLite running in Silverlight”

  1. @Peter I’m embarrassed to share the code, I’ve taken every possible shortcut, but maybe on condition nobody holds it against me 🙂

    Tim

  2. >>I’m embarrassed to share the code
    C’mon Tim, that’s just teasing 🙂 If it helps, the code in this port of HSql in Silverlight is just as dodgy, and available to download full of //HACK: tags! Hsql-in-Silverlight suffers the same problem with isolated storage/locking: doesn’t like to be accessed by two copies of the app.

    An Sqlite-port would be much more attractive from a feature perspective – would love to play with it in Silverlight and look forward to seeing it progress – great work.

  3. Very interesting. Regarding locking and multiple applications, you may be able to use the LocalConnection API in Silverlight 3 to coordinate operations between multiple applications.

  4. Tim,
    You should make your work available for download. Everybody understands that you hacked it just to “get it to work”. There is no shame in that. Other people can then work on it, and see if it can be made to be useful with Silverlight and IsolatedStorage.

  5. Tim,

    Congratluaitons. It might be a “quick hack” but I am sure it can be a good starting point for a cleaner version. Getting it to compile and work is the tough bit, then adding things back in and tweaking can be easier!

    Again, well done. Gary.

Comments are closed.