LoginSignup
2
4

More than 5 years have passed since last update.

c# でDbExecutor を使う

Last updated at Posted at 2015-01-23

最近頻繁にストアドプロシジャーを使いデータを加工してデータを返している。
一時テーブルを使い必要なテーブルから項目を追加すると非常に見やすくしかも高速に実行できる。

そのためC#で簡単にデータを取り扱えないか調べたところ DbExecutor を見つけたのでサンプルコードを書いた。
参考にしたページは下記の通り。

  • DbExecutor ver.2 - C#での生SQL書き補助ライブラリ
  • CodePlex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using SampleDbExecutor.Properties;
using Codeplex.Data;

namespace SampleDbExecutor
{
    class CRow
    {
        public string 患者番号;
        public string 年月日;
    }
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = Settings.Default.XXXXConnectionString;
            var sp = DbExecutor.ExecuteReaderDynamic(
                        new SqlConnection(connStr),
                        "[dbo].[ProcCheckReha]",
                        new { from = "201411", to = "201501" },
                        CommandType.StoredProcedure)
                .Select(d => new CRow
                {
                    患者番号 = d.患者番号,
                    年月日 = d.年月日,
                });

            foreach (var row in sp)
            {
                Console.WriteLine("{0} {1}", row.患者番号, row.年月日);
            }
        }
    }
}

「using Codeplex.Data;」がわからずソースコードをダウンロードした。
私はとてもすっきり書けたと感じている。

ProcCheckReha は、外来リハビリテーション診療料のチェックするためのもので下記の処理を行っている。

  1. 外来診療料、再診料、外来リハビリテーション診察料、リハビリテーション料を算定しているデータを抽出
  2. 1のデータからリハビリテーション料を算定している患者番号を抜き出す
  3. 2で抜き出した患者のみを1から抽出
  4. 3のデータから年月日に展開
  5. 4のデータに診療科、入外区分、算定点数を追加してデータを返す
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4