Noah Hart has ported SQLite3 to C#. While the first port is slower than the original, the project opens the way for SQLite to be used in .NET managed projects without using P/Invoke or unsafe code.

The project, C#-SQLite, is hosted on Google Code and represents the complete porting of SQLite version 3.6.16 to C#, the code being licensed under the Creative Commons 3.0. C#-SQLite passes over 30,000 tests with only 9 still not working. The compiled binary exe is 528KB which is about the same as the original at 506KB. The performance is behind the original native C implementation, but Hart says that he made no attempt to optimize the code yet, and considers the performance as acceptable. All numbers represent Rows/second:

Test C#-SQLite SQLite Insert 300K 1300K Select 1500K 8450K Update 60K 300K Delete 250K 700K

Cory Nelson explains why an SQLite port is better than using other approaches: avoiding P/Invoke which “can be pretty slow and is not portable”. Another reason is that the C code uses “goto all over the place, which probably hampers optimization as opposed to exceptions.”

Many products, including Adobe AIR, contain and use SQLite. Probably the best use of C#-SQLite is for Silverlight projects, as Tim Anderson points out:

Others are hopeful for a local database manager for Microsoft Silverlight, writing to isolated storage. … Silverlight does not allow platform invoke or code marked as unsafe; and while there are apparently only a few p/invoke calls I’m guessing there may be many unsafe sections since the original SQLite makes heavy use of pointers. Although Silverlight is an implementation of the .NET Framework, it does not include the System.Data namespace. It does include System.Linq.

C#-SQLite is not an official version of SQLite, Hart not being affiliated with SQLite.org. Richard Hipp, the creator of SQLite and owner of its trademark, did not accept ”SQLite” to be included in the name of this port, but later agreed with C#-SQLite.

There are other managed databases, some of them being: Perst, db4o, Silverlight Database, System.Data.SQLite.