fluent
fluent

Reputation: 2403

Mono.Data.Sqlite.dll compatibility with SQLite.Net

I built the core logic for my app using a console application in Visual Studio using SQLite.Net, I thought I was simply going to be able to switch out the using statement from

using System.Data.SQLite;

to

using Mono.Data.Sqlite.dll

This process has worked perfectly for other components (Json.Net for example).

However I am finding casing differences between the two Sqlite dll's (SQL and Sql).

System.Data.SQLite

SQLiteCommand command = new SQLiteCommand(sql, DbConnection);
SQLiteDataReader reader = command.ExecuteReader(); 

Mono.Data.Sqlite

SqliteCommand command = new SqliteCommand(sql, DbConnection);
SqliteDataReader reader = command.ExecuteReader(); 

Its easy enough to correct with a global find and replace but its a bit annoying each time I switch between environments to have to do so. Can anyone suggest a solution?

Upvotes: 4

Views: 1616

Answers (2)

Ken
Ken

Reputation: 1985

I solved this by using a second class to abstract the database calls. I have two assemblies containing platform specific versions of a SQLQuery class, one for windows projects and one for monotouch.

My database layer just calls SQLQuery so my calls are something like

SQLQuery q = new SQLQuery("SELECT * FROM foo WHERE bar = @bar");
q.AddParameter("bar", bar);
DataTable dt = q.GetResultsDT();

and this will work on either platform, as long as the SQLQuery assembly for that platform is referenced in the project.

Upvotes: 4

chue x
chue x

Reputation: 18813

One way to get around this is to define a project constant, and then use #if blocks.

Your using:

#if MONO_SQLITE
using Mono.Data.Sqlite.dll
#else
using System.Data.SQLite;
#endif

Your code:

#if MONO_SQLITE
    SqliteCommand command = new SqliteCommand(sql, DbConnection);
    SqliteDataReader reader = command.ExecuteReader(); 
#else
    SQLiteCommand command = new SQLiteCommand(sql, DbConnection);
    SQLiteDataReader reader = command.ExecuteReader();
#endif

Depending on whether you are using Mono or the .Net stack, you then just add or remove the MONO_SQLITE constant definition from your project.

The Mono C# compiler also defines the symbol __MonoCS__. I don't know if MonoTouch does so, but if it did you could also do:

#if __MonoCS__
// ... mono stuff
#else
// ... MS stuff
#endif

Upvotes: 1

Related Questions