Wednesday 1 February 2012

SQLite : The Quick and Dirty Setup for .NET.

1) Download SQLite
While you can get the generic windows binary on the SQLite download page, I’m going to recommend you instead grab the ADO.NET 2.0 Provider for SQLite from sourceforge. I’m not saying this is the most performant version (it does have an ADO wrapper with its attendant malarkey), but it really is a super-easy starting implementation that’s probably good enough for the long haul.
2) Copy the resultant DLL (System.Data.SQLite.DLL) to your project and add a reference.
3) Download and install one of the billions of SQLite GUI clients. I’ve been using the aptly named "SQLite Administrator" (FREE) which has a sweet, Query Analyzer-alike interface. You can find a big list of SLQLite gui clients here http://www.sqlite.org/cvstrac/wiki?p=ManagementTools if you are so inclined.
4) Through the GUI, create a database and make a test table of whatever floats your boat. The result will be a single file with a .s3db extension.
5) There is no step 5! DONE! You can now query, insert, update, delete, create, truncate, etc, to your heart’s content using the System.Data.SQLite ADO wrapper. Here is a little helper db util type class to show you the basic schleck:

using System;
using System.Data;
using System.Data.SQLite;

namespace SqlExample
{
    class Program
    {
        private const string Connectionstring = "Data Source=C:CafeX.s3db";

        public static DataTable GetDataTable(string sql)
        {
            var dt = new DataTable();

            var cnn = new SQLiteConnection(Connectionstring);
            using (cnn)
            {
                cnn.Open();
                var mycommand = new SQLiteCommand(cnn);
                mycommand.CommandText = sql;
                SQLiteDataReader reader = mycommand.ExecuteReader();
                dt.Load(reader);
                reader.Close();
            }

            return dt;
        }

        public static int ExecuteNonQuery(string sql)
        {
            int rowupdate;
            var cnn = new SQLiteConnection(Connectionstring);
            using (cnn)
            {
                cnn.Open();
                var mycommand = new SQLiteCommand(cnn) { CommandText = sql };
                rowupdate = mycommand.ExecuteNonQuery();
            }
            return rowupdate;
        }

        public static object ExecuteScalar(string sql)
        {
            var cnn = new SQLiteConnection(Connectionstring);
            cnn.Open();
            var mycommand = new SQLiteCommand(cnn) { CommandText = sql };
            var value = mycommand.ExecuteScalar();
            cnn.Close();
            cnn.Dispose();
            return value != null ? value.ToString() : null;
        }

        static void Main(string[] args)
        {
            const string sqlinsert = "insert into UserInfo(UserName, FullName) values('hcubiu', 'Michael Lee')";
            ExecuteNonQuery(sqlinsert);
            const string sqlselect = "select * from UserInfo";
            var d = GetDataTable(sqlselect);
            if(d.Rows.Count>0)
            {
                for (var i = 0; i < d.Rows.Count; i++)
                {
                    Console.WriteLine("{0} - {1} - {2}", d.Rows[i]["UserId"], d.Rows[i]["UserName"], d.Rows[i]["FullName"]);
                }
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment