Getting Started with SQLite and Visual Studio 2010

Experimenting with SQLite and C#, your first decision is which version of the overwhelming download options to choose. In my case, I got the sqlite-netFx35-static-binary-bundle-Win32-2008-[version number] running, while the unbundled package raised an error (sorry, can’t remember).

Once you get the correct package, we need a valid file path for our database:

var f = Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 
  "mydatabase.db");
var cn = new System.Data.SQLite.SQLiteConnection(
  (new System.Data.SQLite.SQLiteConnectionStringBuilder { DataSource = f })
    .ConnectionString);
cn.OpenAndReturn();

Next, we need to create the tables if they do not exist:

var tables = cn.GetSchema("Tables", new []{ null, null, "MyTableName", null});        

if (tables.Rows.Count == 0)
{
  var cmd = cn.CreateCommand();
  cmd.CommandText = "CREATE TABLE MyTableName ( ... columns ...)";
  cmd.ExecuteNonQuery();
}

1 thought on “Getting Started with SQLite and Visual Studio 2010

  1. Pingback: Getting Started with SQLite and Loquacious NHibernate 3.2 « devioblog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.