ADO.NET、Dapper、Entity Framework Core を使用して SQLite データベースに接続する方法
C#、.NET、LINQ を使用して SQLite を使用する
必要なもの:
- Visual Studio 2019 以降
- .NET Framework 4.6 以降
- System.Data.SQLite NuGet パッケージ
手順
-
Visual Studio で新しいプロジェクトを作成
- ソリューション エクスプローラーでプロジェクトを右クリックし、NuGet パッケージの管理を選択します。
- NuGet パッケージ マネージャーで、System.Data.SQLite を検索してインストールします。
-
コードの作成
using System;
using System.Data.SQLite;
using System.Linq;
namespace LinqToSQLite
{
class Program
{
static void Main(string[] args)
{
// データベース接続文字列
string connectionString = "Data Source=database.sqlite";
// 接続を開く
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
// テーブルからデータを取得
var query = from customer in connection.Table<Customer>()
where customer.Country == "Japan"
select customer;
// 結果をループ処理
foreach (var customer in query)
{
Console.WriteLine($"{customer.Name} ({customer.Country})");
}
}
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
-
コードの説明
using
ステートメントを使用して、データベース接続を開閉します。from
句を使用して、Customer
テーブルからデータを取得するクエリを構築します。where
句を使用して、Country
列が "Japan" に等しいデータのみを抽出します。select
句を使用して、取得する列を指定します。foreach
ループを使用して、クエリ結果をループ処理します。
-
コードの実行
using System;
using System.Data.SQLite;
using System.Linq;
namespace LinqToSQLite
{
class Program
{
static void Main(string[] args)
{
// データベース接続文字列
string connectionString = "Data Source=database.sqlite";
// 接続を開く
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
// テーブルからデータを取得
var query = from customer in connection.Table<Customer>()
where customer.Country == "Japan"
select customer;
// 結果をループ処理
foreach (var customer in query)
{
Console.WriteLine($"{customer.Name} ({customer.Country})");
}
}
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
このコードを C# ファイルに保存し、Visual Studio で実行します。
実行結果
山田太郎 (Japan)
佐藤花子 (Japan)
ADO.NET は、.NET Framework で提供されるデータアクセス技術です。 ADO.NET を使用して、SQLite データベースに接続し、クエリを実行することができます。
using System;
using System.Data.SQLite;
namespace AdoNetToSQLite
{
class Program
{
static void Main(string[] args)
{
// データベース接続文字列
string connectionString = "Data Source=database.sqlite";
// 接続を開く
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
// SqlCommand オブジェクトを作成
using (var command = new SQLiteCommand("SELECT * FROM Customer WHERE Country = 'Japan'", connection))
{
// データリーダーを取得
using (var reader = command.ExecuteReader())
{
// データリーダーをループ処理
while (reader.Read())
{
Console.WriteLine($"{reader["Name"]} ({reader["Country"]})");
}
}
}
}
}
}
}
Dapper を使用する方法
Dapper は、マイクロ ORM と呼ばれるライブラリです。 Dapper を使用すると、ADO.NET よりも簡単にデータベースにアクセスすることができます。
using System;
using System.Collections.Generic;
using Dapper;
using System.Data.SQLite;
namespace DapperToSQLite
{
class Program
{
static void Main(string[] args)
{
// データベース接続文字列
string connectionString = "Data Source=database.sqlite";
// 接続を開く
using (var connection = new SQLiteConnection(connectionString))
{
// クエリを実行
var customers = connection.Query<Customer>("SELECT * FROM Customer WHERE Country = 'Japan'");
// 結果をループ処理
foreach (var customer in customers)
{
Console.WriteLine($"{customer.Name} ({customer.Country})");
}
}
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
Entity Framework Core を使用する方法
Entity Framework Core は、.NET Framework で提供されるオブジェクト関係マッパー (ORM) です。 Entity Framework Core を使用すると、コードベースとデータベース間のマッピングを自動的に生成することができます。
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EfCoreToSQLite
{
class Program
{
static void Main(string[] args)
{
// DbContext オプションを設定
var options = new DbContextOptionsBuilder<MyContext>()
.UseSqlite("Data Source=database.sqlite")
.Options;
// DbContext を作成
using (var context = new MyContext(options))
{
// クエリを実行
var customers = context.Customers.Where(c => c.Country == "Japan").ToList();
// 結果をループ処理
foreach (var customer in customers)
{
Console.WriteLine($"{customer.Name} ({customer.Country})");
}
}
}
}
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options) : base(options) { }
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
c# .net linq