VistaDB and C#: On Windows and Pi

Welcome to your treat for day 17 of the Gibraltar Software Advent Calendar! Today I’m going to share with you one of the great “ah ha” moments from my Raspberry Pi talk. It comes when I show the audience that you can write software for the Pi, using all the tools that you use right now, and then just copy it across to the Pi and it works. You don’t have to learn all the Linux command line stuff and you don’t have to learn Python. Okay, you *should* do both of those things but, the point is that you don’t have to wait until you have time to do those things, in order to be productive on the Pi.

So, enough talk. Let’s have a demo. Back at day 10 I showed you how to use VistaDB on Mono. In that post we coded everything up on the Pi, but we don’t have to. There’s a much simpler way. We can take advantage of the .Net VM to abstract out the differences between the Win64 and Arm architectures on the two target machines, such that we can code everything on our dev machine and just copy across the required artefacts.

Let’s use the same example as day 10, mainly ‘cos we’re lazy devs. That means a database that looks like this:

2012-12-17-1

And code to “drive” it that looks like this…

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using VistaDB.Provider;
  
  namespace XCopyDeploymentExample
  {
    class Program
    {
      static void Main(string[] args)
      {
        using (VistaDBConnection conn = new VistaDBConnection("Data Source = Data.vdb4"))
        {
          conn.Open();
          using (VistaDBCommand cmd = new VistaDBCommand())
          {
            //GS - Insert
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO Person (Fullname, age) " +
                "VALUES ('Gary', 42)";
            int rowsAffected = cmd.ExecuteNonQuery();

            Console.WriteLine("{0} row(s) inserted.", rowsAffected);

            //GS - Update
            cmd.CommandText = "UPDATE Person SET Age = 24 " + 
                "WHERE FullName = 'Gary'";
            rowsAffected = cmd.ExecuteNonQuery();
            Console.WriteLine("{0} row(s) updated.", rowsAffected);

            //GS - Delete
            cmd.CommandText = "DELETE FROM Person";
            rowsAffected = cmd.ExecuteNonQuery();
            Console.WriteLine("{0} row(s) deleted.", rowsAffected);
          }

          conn.Close();
        }
      }
    }
  }

Running it from Visual Studio gives us the following results:

2012-12-17-2

Now, all we do is compile the solution for release and copy the .exe, the .VDB4 file and the VistaDLL across to the Raspberry Pi (use WinSCP or similar for this).

Running it here gives us the following:

2012-12-17-3

We achieved this without having to compile anything on the Pi! So, you don’t have to wait until you have time to learn the Linux/Python stuff, you can use your existing skills right now!

So come on in, the water’s fine!

That’s all for this post, until next time, happy coding!

The No Hassle Embedded Database for .NET

License your team and deploy worldwide, royalty-free, starting at $1,595/site