Archive for June, 2009

SQLite for C# – Part 3 – My first C# app using SQLite aka Hello World

Sunday, June 28th, 2009

Below we have an equivalent to “Hello World” in SQLite. OK its not really a “Hello World” application – but it does have all the core components to kick start a C# coder to get started with SQLite. Its definitely not elegant, nor the way you would choose to actually do this – but it does run and provides a fair bit of knowledge in a relatively small package.

In order for the code to run you will need to have installed “SQLite.Net” and reference the “System.Data.SQLite” DLL (probably found at "C:\Program Files\SQLite.NET\bin\System.Data.SQLite.dll"). With this and the below code you should have something to get started with.

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

namespace SampleSqliteConsoleDemo
{
   class Program
   {
      static void Main(string[] args)
      {
         // Create table and information using native types
         using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=c:\testperf.db"))
         {
            conn.Open();
            using (SQLiteCommand comm = new SQLiteCommand())
            {
               int limit = 100;
               comm.Connection = conn;
               comm.CommandText = "DROP TABLE IF EXISTS test;" +
                                  "CREATE TABLE IF NOT EXISTS test (n integer);";
               try
               {
                  comm.ExecuteNonQuery();
                  using (SQLiteTransaction tran = conn.BeginTransaction())
                  {
                     // You can use the below in the for loop if you 'need'
                     // to, and it does make it 'easier' to read as you dont
                     // need the additional param code but not only is this
                     // faster, but avoids the potential for SQL injection.
                     //comm.CommandText = "INSERT INTO test values (" + i.ToString() + ")";

                     comm.CommandText = "INSERT INTO test values (@myTestIndex)";
                     DbParameter param = comm.CreateParameter();
                     param.ParameterName = "myTestIndex";
                     param.DbType = DbType.Int32;
                     comm.Parameters.Add(param);

                     for (int i = 0; i < limit; i++)
                     {
                        param.Value = i;
                        comm.ExecuteNonQuery();
                     }
                     tran.Commit();
                  }
               }
               catch (SQLiteException exception)
               {
                  Console.WriteLine("Failed :" + exception.Message);
               }
            }
            conn.Close();
         }

         // Access data using the more traditional ADO.NET namespace
         DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
         using (DbConnection cnn = fact.CreateConnection())
         {
            cnn.ConnectionString = "Data Source=c:\\testperf.db";
            cnn.Open();
            {
               IDbCommand dbcmd = cnn.CreateCommand();
               string sql =
                  "SELECT n " +
                  "FROM test";
               dbcmd.CommandText = sql;
               IDataReader reader = dbcmd.ExecuteReader();
               while (reader.Read())
               {
                  Console.WriteLine("n: " + reader.GetInt32(0));
               }
           }
         }
      }
   }
}

So the things to note are:

  • This this is obviously not a guide for great programming – but it should touch on the highlevels to access information from SQLite.
  • We are using a combination of direct instantiation of “SQLiteConnection” and the ADO.NET base classes. Normally you should try to just use the base classes where possible for portability (should you want to change the DB). We chose both just to highlight the two approaches.
  • The DDL creation uses the SQLite “IF EXISTS” for DROP TABLE and “IF NOT EXISTS” for the CREATE TABLE. These are handy features, but are not standard.
  • Instead of using the commonly found string concatenation approach to execute the SQL (most introduction web examples show this) we show the preferred approach using DbParameter. This is not only safer (avoids SQL Injection), but is faster as well! Hard to complain about faster and more secure!

Related Links:

Need some guidance to speed up your web rendering?

Thursday, June 25th, 2009

Or need to check that what you have done in the performance area matches others? Here are some really good pages for improving your web page rendering:

  • Best Practices for Speeding Up Your Web Site
    • Definitely read this article (all the way), even if you think you know the first couple of recommendations there are some less well known ones towards the end.
  • How we improved performance on Google Code
    • This covers how Google Code performance was increased – its always interesting when you see how “Giants” solve common problems.
  • Yahoo! YSlow
    • YSlow analyzes web pages and suggests ways to improve their performance based on a set of rules for high performance web pages. YSlow is a Firefox add-on integrated with the Firebug web development tool.
  • Exceptional Performance
    • Yahoo!’s Exceptional Performance team evangelizes best practices for improving web performance.

And for any of those out there using YUI the “YUI 3.0.0 beta 1 Available for Download” has just been made available.

Gareth

Want to install SQL2008 on Windows 2008 RC2 (or Windows 7)?

Thursday, June 25th, 2009

Unfortunately its not as simple as you would think, SQL Server 2008 install requires the .NET 3.5 framework to be installed or will install it as part of the pre-req checking.   Changes in Windows 7 can prevent SQL Server 2008 from installing the .NET 3.5 framework pre-requisite.   Instead you may need to install the framework separately using the “Run this program in compatibility mode”.  Read here for more information.

UPDATE 2009-06-29: It seems there are similar issues with SQL2005 as well, have a look SQL Server version has a known compatibility issues with Windows 7 RC. This warning is genuine and applies to all SQL Server editions, including all Express editions.

Gareth

NHibernate News

Tuesday, June 23rd, 2009

It seems NHibernate team is turning out some pretty cool new stuff at the moment:

If you have not looked at NHibernate now may be the time,

Gareth