
[CSharp]SQL Compact3.5 でローカルデータベースに追加/削除/更新するサンプル#tryLocalDB

ADO.NET Entity Data Modelとローカルデータベース(SQL Compact 3.5)使ってデータベースへの追加、削除、更新
普通はそのままリストにデータバインドして使うらしいけど、一時的なデータのバッファみたいに使いたいのでSQLがやっぱ便利。
ちなみにSQLCompactなのでidの自動インクリメントみたいなものは使えません。使うとエラー出るので自分で作る。
SQLの実行時のエラーはException#MessageではとれなくてException#InnerException#Messageを見るととれる
デバッグビルドでデータが実際に書き込まれているのは”tryLocalDB\bin\Debug\fmwork.sdf”このファイル
●データーベース、ADO.NET Entity Data Modelの作り方
①プロジェクト名(tryLocalDB)右クリック→追加→新しい項目→ローカルデータベース
②プロジェクト名(tryLocalDB)右クリック→追加→新しい項目→ADO.NET Entity Data Model
③モデルを含めるコンテンツでデータベースから作成
●検索した
クエリ ビルダー メソッド 結果 順番に取り出す
DeleteObject クエリ ビルダー メソッド
●開発環境
Visual Studio 2010 Professional SP1.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Diagnostics; using System.Data.Entity; using System.Data.Objects; namespace tryLocalDB { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } // //////////////////////////////////////////////////////////// // データベースの内容を表示する private void button_Show_Click(object sender, RoutedEventArgs e) { using(fmworkEntities entitiy = new fmworkEntities()) { try { // これで全部のデータベースの内容を取り出せるらしい ObjectQuery<follow> result = entitiy.follows; // 順番に取り出す string msg = ""; int count = 0; foreach (follow curr in result) { msg += "follow[" + count + "]= owner_id: " + curr.owner_id + " user_id: " + curr.user_id + " time: " + curr.follow_time.ToString() + "\n"; count++; } MessageBox.Show(msg); // //////////////////////////////////////////////////////////// // ここから下はブレイクポイントを張って中身を見てください DateTime dt = new DateTime(2012, 09, 03, 0, 40, 12); ObjectParameter objParam = new ObjectParameter("time", dt); // クエリを直接実行 string query = "SELECT VALUE follow FROM fmworkEntities.follows AS follow" + " WHERE follow.follow_time < @time"; ObjectQuery<follow> res = entitiy.CreateQuery<follow>(query, objParam); // クエリを直接実行 ↓①抽出したいモノを変えると query = "SELECT VALUE follow.user_id FROM fmworkEntities.follows AS follow" + " WHERE follow.follow_time < @time"; ObjectQuery<Int64> res3 = entitiy.CreateQuery<Int64>(query, objParam); // ↑ ↑ // ②それにあわせて戻り値の型も変わるので注意 // クエリをメソッド使って実行 // こいつらのことをクエリ ビルダー メソッドって言うらしい。 //res = entitiy.CreateQuery<follow>(query, objParam); // "it"ってのはテーブルの名前のこと(follow.follow_timeみたいなものかな?)これ指定しないとエラーになるよ。 ObjectQuery<follow> res4 = entitiy.follows.Where("it.follow_time < @time", objParam); // 最初の1個だけ取り出すとか、他にもいろいろあります。 //follow res5 = entitiy.follows.First<follow>(); } catch (Exception exp) { MessageBox.Show(exp.Message); } } } // //////////////////////////////////////////////////////////// // データベースにデータを追加 private void button_add_Click(object sender, RoutedEventArgs e) { using(fmworkEntities entitiy = new fmworkEntities()) { Random rand = new Random(); try { // テーブルに新しいレコードを追加 follow entry = new follow() { follow_time = DateTime.Now, owner_id = rand.Next(65565), user_id = 3928101382 }; entitiy.follows.AddObject(entry); // これしないとデータベースに反映がされない entitiy.SaveChanges(); // 適当に追加してると、かぶる値がでるからエラーでこっちとんでくるよ } catch (Exception exp) { Debug.Print(exp.Message); // EntitySQLでエラー出た場合はInnerException使わないと内容が見えない Debug.Print(exp.InnerException.Message); //MessageBox.Show(exp.Message); //MessageBox.Show(exp.InnerException.Message); } } } // //////////////////////////////////////////////////////////// // データベースの中身をクリア private void button_clear_Click(object sender, RoutedEventArgs e) { using(fmworkEntities entitiy = new fmworkEntities()) { try { ObjectQuery<follow> deleteList = entitiy.follows; // 全部消す(削除マーカーをつけるだけ、SaveChanges呼ばれるまでは反映されない) foreach (follow curr in deleteList) { entitiy.DeleteObject(curr); } // これしないとデータベースに反映がされない entitiy.SaveChanges(); // 適当に追加してると、かぶる値がでるからエラーでこっちとんでくるよ } catch (Exception exp) { Debug.Print(exp.Message); // EntitySQLでエラー出た場合はInnerException使わないと内容が見えない Debug.Print(exp.InnerException.Message); //MessageBox.Show(exp.Message); //MessageBox.Show(exp.InnerException.Message); } } } } }
GPS/ネットワークから座標を取得するサンプル#tryGPS00 [Android]ファイル/フォルダ選択ダイアログのサンプル#tryFileOpenDialog00