SQLite on C# Cross-Platform Applications - c#

Can someone help/guide me with using SQLite lib on Linux (MONO) and Windows (.NET)
On linux i use native mono sqlite client, and on windows i use http://sqlite.phxsoftware.com/
is there a way to define 'using' directives like this :
#if (linux)
using Mono.Data.Sqlite;
#else
using System.Data.SQLite;
Another problem is small differencies on both implementations, like :
cmd = new SqliteCommand(); // mono
cmd = new SQLiteCommand(); // sqlite.phxsoftware.com
Waiting for any help
If you know better or simplier way to do this it'll very thankfull for info.
Thanks

You can use csharp-sqlite which is a port to C# of Sql-Lite. It is very active and based on 3.6.22 version of SqlLite. See Miguel's comments on attempts to try to speed it up.

I've recently come across the issue too: building an application that uses Sqlite on Windows with Visual Studio and deploying it on an Ubuntu Server box for production.
The simplest solution I've found is using the Mono driver for Sqlite: Mono.Data.Sqlite.
Things could have been a little simpler but there is a bug with .Net 4.0 that is not yet packaged in the official Mono releases.
So you'll have to compile Mono from source (the general instructions are here):
first compile the whole Mono stuff
you do not need to install it if you want to keep your current Mono setup
copy the Mono.Data.Sqlite.dll library
Of course you can "cross-compile": I've built Mono on Ubuntu Server and used the dll in a Windows .Net project.
Then ensure you have the native Sqlite library (sqlite3.dll for Windows and sqlite3.so for Linux) in your library path: for Windows I simply copied the sqlite3.dll next to the Mono.Data.Sqlite.dll assembly, for Linux it should work out of the box.
You project should then work seamlessly in both Windows/.Net and Linux/Mono environments.

You can solve the naming differences using alias
#if (linux)
using SqlCommand = Mono.Data.Sqlite.SqliteCommand;
#else
using SqlCommand = System.Data.SQLite;
Using different assemblies for different builds is a more complex task i think.. you can have a look at the MSBuild documentation

There is a fully managed SQLite translation. If you use that, then you could use the same DLL on Mono and Windows.
Another way to solve your issue is to create your own database-interface and then implement that interface once for Mono and one for Microsoft .NET in separate DLLs. (Basically the same way you create code that runs against different databases)

You can just use the mono implementation of SQLite for both, Windows and Linux versions of your software. Just include the mono assembly for SQLite in your software package and refer to it locally.
As you can read here in the last post, you can use the managed way of mono solely in your code and need just to redistribute the native part for windows differently. But you do not have to mess with to managed implementations and redundand code through that.

The open-source Vici CoolStorage ORM library works on Windows (.NET), Mono (Mac,Linux and Windows) and MonoTouch (iPhone) using that platform's SQLite driver.
To use it on these different platforms, you don't have to change anything to your source code. Just recompile, and it should work.

IMO you should first try to find an implementation that works in both Windows and Linux. If that doesn't work, create an assembly that defines a common interface for SQLite and put all you "#if LINUX" code in that assembly. Then use that assembly in the main application to avoid cluttering the main app with all the # defines.

The SQLite ADO.NET provider is actually a mixed-mode assembly, which contains the native SQLite library. This native library is not the same on Windows and Linux of course, so this provider doesn't work on Linux. However, there is a managed-only version of the provider (SQLite-1.0.65.0-managedonly-binaries.zip on the download page). So I think you just need to use this version of the provider, and provide the adequate native SQLite dynamic library along with it (.dll on Windows, .so on Linux)

Some answers suggest the fully-managed C# port of SQLite3. But, unfortunately, no release to date supports Linux or Mac OS X despite being compilable with the Mono C# compiler.
A future release will correctly run on those non-Windows platforms with the Mono runtime. WIth some caveats, the source repository contains code that works.
Having said that, the System.Data.SQLite implementation available from sqlite.org works with both .Net and Mono, on Windows and non-Windows platforms. You just need to ensure that the app.config used by Mono at runtime maps the (C++) SQLite3 dll to the appropriate .so or .dylib library. If you choose the "mixed-mode" version, then it should just work and you don't need to worry about separate dlls.

Related

Is it possible to use DLLs with Visual Studio Mac

I have an application that uses a third party DLL. Is there a way in Visual Studio for Mac to write an application to access it the same way as I can on windows?
It depends:
Managed .NET DLL:
Can be imported and used the same way as in VS on Windows
Native DLL: can't be used directly. You'll need to build it for your target system, OSX in your case.
If you have C/C++ code you should be able to build it for OSX (with GCC for example) if it doesn't have some foreign (Windows) platform specific code. Then you can use the compiled *.so / *.dylib file directly. But you'll need to tell the .NET Runtime (Mono for example on OSX) to use the different file using a DllMap configuration file (see http://www.mono-project.com/docs/advanced/pinvoke/dllmap/ for examples).
The sources you've linked look like they're for Unix, so the chance to be able to build them on OSX are pretty good (there's a Makefile and the resulting binary would be libswe.so on Unix). You could try to pass the -dynamiclib parameter to GCC to get a OSX specific libswe.dylib (What are the differences between .so and .dylib on osx? seems to be a good answer about dylib)
If you have the binary of your DLL for OSX, you just need a configuration file for your .NET application, which could look like this:
<configuration>
<dllmap os="osx" dll="libswe.dll" target="libswe.dylib"/>
<configuration/>
It tells the .NET Runtime to import the symbols from libswe.dylib instead of libswe.dll if the current OS is OSX.
No.
Although you might think that because you have an IDE with the same name as the Windows counterpart the binaries it produces are not transferable.
An answer on Super User gives some more information:
There's no real DLLs in OS X, Linux, or any POSIX for that matter.
They don't make the differentiation.
Why?
A lot of Mac stuff, for one, is self-contained (.app's are really just
folders after all).
Although...
Another queston on SO asks the same thing, and an answer says:
Finally Microsoft released .NET Core which is completely platform
independent.
... now .NET applications can be developed on Mac or Linux machine
using the lightweight IDE Visual Studio Code and Visual Studio for Mac
IDE has been released where Mono on MacOS X is integrated.
So if the supplier of the library (DLL) you want to use has created a version of it with the .NET Core framework, you might be in luck.

Possible to create an application c# with no framework

for strict constrains in my scenarios, I have very few room to install my application, and .net framework is not installable (any version).
If the application is really simple (more or less), it is possible to create an application in visual studio (in c#) with no dependendency from the .net framework?
Thanks!
Short answer: NO.
There is no way to create a .NET application without any framework.
It is possible to compile a C# app such that it has no dependencies on any of the built-in .NET types & libraries, by using the /nostdlib switch (see http://msdn.microsoft.com/en-us/library/fa13yay7.aspx). You then need to supply your own System namespace.
However, this doesn't remove the need for the .NET framework on the target machine if you use the standard C# compiler. As well as containing the built-in types, the framework also includes the JIT IL compiler, the CLR extra, which all .NET executables and dll's are reliant on.
There are ways of compiling C# code such that it doesn't need the framework though. The Xamarin product for example (http://xamarin.com/), supports compiling C# code to native iOS apps, which are wholely independent of the .NET framework. I'm not aware of any equivalent for "desktop" OS's though.
Writing, compiling and running a C# program without .Net means running a special C# compiler that produces native code instead of managed code. I think such a compiler exists from WinRT for mobile phones, which uses COM instead of .Net (And C++/CX instead of C++/CLI). Code it produces does not depend on the .Net Framework, but does depend on the WinRT runtime.
You may create mono GTK# application and then use mkbundle to generate independent executable. You can use Visual Studio to build your logic and use Xamarin studio to build GTK# GUI. For more information about mkbundle see this and this.
To reply to your query. It really is not possible to create a .Net application without the .Net frame work. And moreover if you have installed Visual Studio by default it would have asked you to install .Net framework or would have installed it by default. In that scenario there is already .Net framework installed in your PC.
Thanks

dlls on windows and dylibs on macintosh

I have joined a project that has been written in xcode using objective-C that is using some frameworks. This project is aimed at adding Wii-mote functionality to both the windows and Macintosh versions of Unity3d.
My question is how should I go about getting a windows dll that mirrors the functionality of the Macintosh .bundle/.dylib?
Do I need to start translating the code from Objective-C to C#?
or,
Do I need to find/code my own windows .dll from scratch?
or,
Is there a library type that is compatible with both windows and Macintosh?
Thanks in advance for your help!
There is not a library type that is compatible with both systems. It might be possible to use C# libraries on both systems if you're using Mono on the Mac to run C# code, but you already stated that it's an Objective-C project, so I'm guessing that option is out.
So yes, you'll need to write your own .dll on the Windows side in your language of choice.

Converting .NET App to x86 native code

There's a program written entirely in C# that targets .NET Framework 2.0.
Is there a way I could somehow compile (translate) managed EXE to a native one so it could be .NET-agnostic? I know there are probably commercial products for that purpose... but they are a bit expensive.
The problem is that we are to deploy the program on computers running Windows XP with no .NET Framework installed. There's also a requirement that the program's size must not exceed 500Kb (1Mb maximum) for it is downloaded from the web server (now the size is 255Kb). That is why there's no way we could attach a full-fledged .NET FX (or even a reduced one) to the downloaded program's file.
Obviously it is a terrible software engineering error that should have been detected and avoided earlier so we could use native technologies like C++ instead.
We have tried for now Novell's Mono - an open-source implementation of .NET Framework for Linux, MAC and Windows. Mono consists of C# Compiler, IDE, runtime (CLR) and Class Library assemblies (like System.dll and mscorlib.dll - much like .NET's class library assemblies installed to GAC).
What we tried to do is to locate CLR files and ship those along with our program's file and a few assemblies. This way the program can be invoked by running "mono program.exe" (command prompt) on a user's computer.
In addition to the inconvenience of such a use for the end user CLR files (mono.exe and mono.dll) turned out to be about 2.5 Mb in total that is much greater than the desired 500 Kb or even 1 Mb.
So, we have left with no other option but to translate our .NET App to a native one by a compiler, however the question remains - what compiler should we use and where could we find one...
For now I have stumbled upon a Singularity OS Project by Microsoft Research. It is an open-source research OS that is written in managed code (in part at least). The Singularity OS includes a Bartok compiler that the OS uses in order to translate a managed program to a native one (x86 32 bit). It should be noted that Bartok can't translate all the aspects of .NET 2.0 to a native code, but most of them. However I haven't yet learnt how to use the Singularity...
I would be really grateful to you if you could provide me with some useful tips and advice regarding the problem, your own experience with Singularity OS and Bartok Compiler or another approaches to the problem that I have overlooked and ways of solving it.
Thank you very much in advance!
Finally, using Mono's Full AOT feature (on Callum Rogers' advice) I've managed to produce a program.exe.dll that lacks a CLI header.
So it looks to me like a native dll. However I can't figure out how to convert that dll into exe or make it operational.
Also this dll doesn't seem to expose any functions of interest such as main function.
Check out AOT (Ahead Of Time) Compilation from the Mono project. This compiles your managed project into a native exe or an elf executable (depending on which system you target) that does not need the JIT. This is the technique used to get mono apps onto the iPhone (where the JIT/Framework are not allowed) and also has the added benefits of faster startup times, lower memory usage and it makes it harder for people to decompile your code. You said you were already using Mono, so it should be compatible.
Read up about it at the mono-project.com website and at Miguel de Icaza's blog (and iPhone info).
Note that you cannot use dynamic code or generic interfaces like
interface IFoo<T> {
...
void SomeMethod ();
}
And you will have to compile the DLLs of all the libraries you use.
PS: Make sure to use "Full" AOT for your problem.
2018 Update
At Build 2018, Microsoft announced .Net Core 3.0 roadmap that support Windows desktop applications (Winform & WPF)
2017 Update
For console apps, you can use .net core Self-contained deployments (SCD). Even for a hello world app, your package will 50MB+. You still need to install VC runtime though.
Update
As #jenix's comment, .NET Native is only for Windows Store Apps(UWP). After 3 years of it's announcement, this is still true, .net native for desktop may be dropped by microsoft . So this answer is not applicable anymore.
========
Microsoft Announced .NET Native Preview on Build 2014
With the .NET Native Developer Preview, apps will get deployed on end-user devices as fully self-contained natively compiled code, and will not have a dependency on the .NET Framework on the target device/machine. So, no .NET framework required on the target machine with .NET Native.
Announcing .NET Native Preview
Microsoft .NET Native
There is a project called CrossNet that parses .Net Assemblies and generates unmanaged C++ code, that can be compiled in any standard compiler.
Not really a solution for .NET to native conversion, but maybe this helps: http://www.yoda.arachsys.com/csharp/faq/#framework.required
Not quite sure that there is much you can do besides painstakingly rewrite the application. To ease the already burdening process, you could disassemble the .NET application using something like Reflector (into Microsoft C++), and use that as a base to start and just replace managed C++ references with native ones.

Can you compile C# so it doesn't need the .NET Framework at runtime?

Is it possible to force the C# compiler to pull all the referenced calls out of the framework and pack them into dlls or even a single executable?
I like writing quick 'one-off' applications with C#, however I don't want to have to install the whole framework on the target machine once it's ready to go.
You ask a loaded question. C# is merely a language and does not require the .NET Framework. The process of compiling it requires a compiler, which may or may not itself take a dependency on the .NET Framework (Microsoft's C# compiler does not -- it is written in native code). Your program will need to reference some assembly where types, classes, and methods can be found for your use. You can remove system.dll and mscorlib.dll from your references list and reference your own assemblies. So you can avoid dependencies on the .NET Framework if you really work at it. But in the end, unless you have a C# compiler that compiles programs to native code you still have a dependency on the CLR.
That's a very technical way of saying... almost nothing. But it answers your question. :) More practically useful however is how to get your C# programs to run with a minimum of dependencies. mkbundle from mono will actually let you compile it all into an .exe with virtually no dependencies.
But if you want to stick with the Microsoft .NET Framework, you can achieve a much lighter footprint and faster install of the dependencies you commonly need by using the Client profile of .NET 3.5 SP1. You can read about it here:
http://msdn.microsoft.com/en-us/library/cc656912.aspx
Look at mkbundle using Mono.
It is now possible to compile C# to native code using Microsoft .NET Native:
https://msdn.microsoft.com/en-us/library/dn584397(v=vs.110).aspx
It automatically compiles the release version of apps that are written in managed code (C# or Visual Basic) and that target the .NET Framework and Windows 10 to native code.
...
For users of your apps, .NET Native offers these advantages:
•Fast execution times
•Consistently speedy startup times
•Low deployment and update costs
•Optimized app memory usage
This only works with Visual Studio .NET 2015.
Take a look at the .NET client profile.
This will allow you to package a minimum install on the client machine.. which will later be updated by windows update to the full framework.
This depends, of course, on your app only using libraries that are contained in the client profile ...
Some info here: http://blogs.windowsclient.net/trickster92/archive/2008/05/21/introducing-the-net-framework-client-profile.aspx
It's said it is possible, using 3rd-party tools such as http://www.remotesoft.com/linker/
Not possible. Your "compiled" C# application is a language which the .Net CLR interprets (should have said JITed, Reads the IL, compiles to native code, and then invokes the compiled native code) at runtime.
FYI .net 2.0 is a standard install on xp SP2 and vista, so you won't be paying that much of a penalty.
You could look into mono, but this still involves running some kind of framework on your target machine.
This dependency which unfortunately frequently breaks or is missing in the real world is a big reason why C# has not had a wider adoption. On the flip side most development does have dependencies.. look at C++ & Java for example.
I don't think we will really get away from these dependency issues anytime soon, so I recommend that if you want to use C#, that you make a wrapper for installation which checks for the .net framework version dependency you need, and if missing notify the user that they need this to run your app.
Some C# features are bound to interfaces of the .NET framework.
For example:
yield return requires the IEnumerable interface
using (x) {} requires the IDisposable interface

Categories

Resources