LoginSignup
7

More than 5 years have passed since last update.

C#:ASP.NET COREでのDBアクセスについて(ADO.NET SqlClient/Dapper)

Last updated at Posted at 2019-01-23

始めに

 これまでまともにORマッパーを利用したことがなかったので、ADO.NET SqlClientで作成したものをDapperで作り直してみました。
 両方作成してみて、ようやくORマッパーのメリット(可読性能、メンテナンス性能)を感じることができました。

内容

 画面遷移時に、SELECTした内容をテーブル構造で表示します。
 肝心のModelの部分だけでいいよ、文章長いよという方もいらっしゃると思いますが、自分だったらMVC全て記載されていた方が嬉しいので記載します。

環境

 ■VisualStudio2017
 ■SQlServer2016

コード

共通

 Controllerはどちらのパターンでも同一です。ModelとViewが分岐します。

Controller_HomeController.cs
public IActionResult Hoge()
{
    ViewBag.Shiire = Models.Hoge.Get_Shiire();
    return View();
}

ADO.NET SqlClient版

 SELECTしてきた結果をList in ListにReadしながら詰め直しています。

Model_Hoge.cs
public static List<List<string>> Get_Shiire()
{
    List<List<string>> shiire = new List<List<string>>();
    using (var con = new SqlConnection("DB接続文字列"))
    using (var cmd = new SqlCommand("SELECT 仕入先CD,仕入先名,取引メモ FROM tb_仕入先マスタ", con))
    {
        con.Open();
        using (var dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                List<String> Rowdata = new List<String>
                {
                    dr["仕入先CD"].ToString(),
                    dr["仕入先名"].ToString(),
                    dr["取引メモ"].ToString()
                };
                siire.Add(Rowdata);
            }
       }
       con.Close();
   }
   return siire;
}
View_Hoge.cshtml
<table class="table">
    <thead>
        <tr>
            <td>仕入先CD</td>
            <td>仕入先名</td>
            <td>取引メモ</td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item1 in ViewBag.Shiire)
        {
            <tr>
                <td>@item1.[0]</td>
                <td>@item1.[1]</td>
                <td>@item1.[2]</td>
            </tr>
        }
    </tbody>
</table>

Dapper版

 マップ先(Shiire)を用意してQueryで直接詰めています。

Model_Hoge.cs
using Dapper;
public class Shiire
{
    public string 仕入先CD { get; set; }
    public string 仕入先名 { get; set; }
    public string 取引メモ { get; set; }
}

public static List<Shiire> Get_Shiire()
{
    using (SqlConnection connection = new SqlConnection())
    {
        connection.ConnectionString = "DB接続文字列";
        connection.Open();
        string sql = "SELECT 仕入先CD,仕入先名,取引メモ FROM tb_仕入先マスタ";
        return connection.Query<Shiire>(sql).ToList();
    }
}
View_Hoge.cshtml
<table class="table">
    <thead>
        <tr>
            <td>仕入先CD</td>
            <td>仕入先名</td>
            <td>取引メモ</td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item1 in ViewBag.Shiire)
        {
            <tr>
                <td>@item1.仕入先CD</td>
                <td>@item1.仕入先名</td>
                <td>@item1.取引メモ</td>
            </tr>
        }
    </tbody>
</table>

感想

 今回のパターンでは取得データが全てStringなので差が出ませんでしたが、キャストミスなどが発生しにくい分Dapperの方が好みだなと思いました。

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
7