Table of Content
新しいc#プロジェクトを作成します。
NuGet (.NET Framework用オープンソースライブラリの管理ツール)を使って、ご要望のライブラリをインストール・・・。
したいのですが、
VisualStudio 2013のNuGetは、バージョンv2.12.0が最新、
Visual Studio 2015ならば、 バージョンv3.6.0が使える状況(2019.12.26現在) 。
無いものは仕方ないので、バージョンv1.xや バージョンv2.xで使えるものを以下から探す。
NuGet Gallery| Packages にアクセス。
今回は、System.Data.SQLite.Core v1.0.112をsearchする。
タブを切り替えて「Package Manager」でのインストール方法を確認する。Install-Package System.Data.SQLite.Core -Version 1.0.112
開きっぱなしの VisualStudio 2013に戻って、
メニュー[表示]-[その他のウィンドウ]-[パッケージマネージャーコンソール]を選択。
開いたウィンドウのPM>プロンプトで先ほどのコマンドを入力。
Install-Package System.Data.SQLite.Core -Version 1.0.112
インストール完了。
お疲れ様でした。ついでに、SQLite3とADO.NETでのサンプルコード
using System; using System.Data.SQLite; using System.Windows.Forms; namespace SQLiteApplication { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string db_file = @"TESTDATABASE.db"; // データベース作成 private void button1_Click(object sender, EventArgs e) { // コネクションを開いてテーブル作成して閉じる using (var conn = new SQLiteConnection("Data Source=" + db_file)) { conn.Open(); using (SQLiteCommand command = conn.CreateCommand()) { command.CommandText = "CREATE TABLE T_SAMPLE(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INTEGER)"; command.ExecuteNonQuery(); } conn.Close(); } } // データベース接続 private void button2_Click(object sender, EventArgs e) { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + db_file)) { try { conn.Open(); MessageBox.Show("データベース接続 成功", "Connection Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { MessageBox.Show(exception.Message, "データベース接続 失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // データの追加 private void button3_Click(object sender, EventArgs e) { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + db_file)) { conn.Open(); using (SQLiteTransaction trans = conn.BeginTransaction()) { SQLiteCommand cmd = conn.CreateCommand(); // インサート cmd.CommandText = "INSERT INTO T_SAMPLE (Name, Age) VALUES (@Name, @Age)"; // パラメータセット cmd.Parameters.Add("Name", System.Data.DbType.String); cmd.Parameters.Add("Age", System.Data.DbType.Int64); // データ追加 cmd.Parameters["Name"].Value = "佐藤"; cmd.Parameters["Age"].Value = 32; cmd.ExecuteNonQuery(); cmd.Parameters["Name"].Value = "斉藤"; cmd.Parameters["Age"].Value = 24; cmd.ExecuteNonQuery(); // コミット trans.Commit(); MessageBox.Show("データの追加 成功", "Connection Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } // データの取得 private void button4_Click(object sender, EventArgs e) { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + db_file)) { conn.Open(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM T_SAMPLE"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { string message = "Id,Name,Age\n"; while (reader.Read()) { message += reader["Id"].ToString() + "," + reader["Name"].ToString() + "," + reader["Age"].ToString() + "\n"; } MessageBox.Show(message); } conn.Close(); } } } }
コメント