C#でSQLite接続

C#(.NET環境)で利用できるSQLiteとしてSystem.Data.SQLite.dllがあります。
System.Data.SQLite.dllは、ADO.NETアダプタです。そして、System.Data.SQLite.dllは、SQLiteのデータベースファイルにADO.NET接続するためのメソッドだけでなく、System.Data.SQLite.dllには、SQLite.exeが含まれているので、別途SQLite.exeを用意する必要がありません。(System.Data.SQLite.dllからSQLiteのデータベースファイルを作成することが出来ます。)
System.Data.SQLite.dllは、「System.Data.SQLite」のページでダウンロードをすることが出来ます。
開発環境や実行端末で使用されている.NET Frameworkのバージョンに合わせたSystem.Data.SQLite.dllを参照する必要があります。
EXEファイルの実行時は、System.Data.SQLite.dllとSystem.Data.SQLite.xmlが必要となります。実行するEXEファイルと同じフォルダなどの実行パス通っているフォルダにSystem.Data.SQLite.dllとSystem.Data.SQLite.xmlを配置しなければ実行時にエラーが発生しますので注意が必要です。

C#でSQLite接続のサンプルソース

SQL文を実行し、検索結果が格納されるDataTableを返します。

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

namespace Tool.Common.Db
{
    /// <summary>
    /// 機能: データベース処理の共通クラス
    /// </summary>
    public class SQLiteDd
    {
        private string connectionString = "Data Source=C:\\data.db";

        /// <summary>
        /// SQL文を実行し、検索結果が格納されるDataTableを返す
        /// </summary>
        /// <param name="sql">実行用のSQL文</param>
        /// <param name="parameters">SQL実行用の引数</param>
        /// <returns>検索結果</returns>
        public DataTable ExecuteDataTable(string sql
            , SQLiteParameter[] parameters)
        {
            using (SQLiteConnection connection
                = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand command
                    = new SQLiteCommand(sql, connection))
                {
                    if (parameters != null)
                    {
                        command.Parameters.AddRange(parameters);
                    }
                    SQLiteDataAdapter adapter
                        = new SQLiteDataAdapter(command);
                    DataTable data = new DataTable();
                    adapter.Fill(data);
                    return data;
                }
            }
        }
    }
}

inserted by FC2 system