Posts tagged ‘Mono’

Mono on the Mac

As great as Mono can be allowing you to run .net application under Win, Linux, Mac and more.  There is a gotcha, if you intend to run on the Mac you can not use the Win forms.  As the Win forms have limited functionality on the Mac or just don’t run,  you are also required to install X11 on the Mac for starters.  What you will have to do is to use the Gtk# UI libraries instead as they are by far more mature in Mono on the Mac.  The strange part is that the Win forms work fine on the linux desktop under Mono.

This doesn’t make any sense, does it ?    If you want cross platform UI application that you intend to run under Mono including on the Mac you need to write it that way from the start, use Mono and Gtk# to develop.  That means for you MS VS .net developers you can’t use your favorite VS editor, at least not out of the box.  You have to get something else like MonoDevelop.   You also have to think about it if you have an old application that you want running under Mono on the Mac, you will have to go in and rip out the Win forms and replace them with the Gtk# forms.

As they put it on the Mono website,

There were two previous attempts to implement System.Windows.Forms in Mono. These initial attempts were done on top of other toolkits: first Gtk, then Wine. Each one had its share of problems, causing it to be abandoned.

Read more…

I’m not bashing Mono by any means, I think it’s a great effort and well worth investing some time using Mono.  Even getting involved if your so inclined.  There is just that gotcha about using Win Forms that I wanted to point out to the unaware Win C# developers.

SQLite in C#

As one of my readers pointed out, SQLite is a small database fit for programs that need only minimal database activity looking for small footprint database. It’s used by Firefox / google chrome browser among others.

First let’s take a look at creating a small test SQLite database.  From the command prompt start up SQLite command line tool.

>sqlite3 test.db
sqlite>CREATE TABLE cars ( id INTEGER PRIMARY KEY AUTOINCREMENT, model text, year integer );
sqlite>insert into cars( model, year ) values( “Ford 350”, 2007 );
sqlite>insert into cars( model, year ) values( “Buick Skylark”, 1953 );
sqlite>insert into cars( model, year ) values( “Honda Civic”, 2002 );
sqlite>Select * from cars;
1|Ford 350|2007
2|Buick Skylark|1953
3|Honda Civic|2002
sqlite>.quit

You can activate the help by issuing .help
sqlite>.help

With the database created lets access it from C#. First you need some kind of a .net SQLite wrapper library. I choose to use system.data.SQLite it pretty much wraps the whole SQLite engine, it also works under Mono. All you need is their dll, add it to your project and reference the SQLite namespace. When you ship just include the System.Data.SQLite.DLL and your all set.

using System.Data.SQLite;

// Connection to the database
using( DbConnection conn = new SQLiteConnection(“Data Source=test.db”))
using( DbCommand cmd = conn.CreateCommand())
{
    // Open the database
    conn.Open();
    // Insert to the table
    cmd.CommandText = “insert into cars( model, year ) values( ‘Hummer’, 2000 )”;
    // exec
    cmd.ExecuteNonQuery();

    // Select from the table
    cmd.CommandText = “SELECT * FROM cars”;
    using (DbDataReader reader = cmd.ExecuteReader())
    {
        while( reader.Read() )
        {
            Console.WriteLine(“id=” + reader[0] +
                   “, model=” + reader[1] + “, year=” + reader[2]);
        }
    }
}

The SQLite dll is about 820k that’s the only software you need when you deploy. Along with the database, unless you want to create the database on the fly. The upside is that now you have a neat little database at your disposal. It’s fast and has a small footprint, which is what we were looking for.

But… yes there is always one of those. It turns out the Mono version is totally another distro, so don’t expect to fold this in your C# progam and expect it to run under Mono also. See further details on that here.