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

I built the main logic for my application using the console application in Visual Studio using SQLite.Net, I thought that I would just be able to disable the using statement from

using System.Data.SQLite; 

to

 using Mono.Data.Sqlite.dll 

This process works great for other components (e.g. Json.Net).

However, I find differences in the casing between the two Sqlite dlls (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(); 

It’s easy enough to fix with global find and replace, but it’s a little annoying every time I switch between environments to do this. Can anyone suggest a solution?

+4
source share
2 answers

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

My database level just calls SQLQuery , so my calls are like

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

and this will work on any platform if the SQLQuery assembly for that platform is referenced in the project.

+4
source

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

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 use the Mono or .Net stack, you simply add or remove the MONO_SQLITE constant MONO_SQLITE from your project.

The Mono C # compiler also defines the __MonoCS__ character. I don't know if MonoTouch does this, but if it does, you can also do:

 #if __MonoCS__ // ... mono stuff #else // ... MS stuff #endif 
+1
source

Source: https://habr.com/ru/post/1482161/


All Articles