C#/Winform

[C#/Dapper] Dapper 설치 및 기본사용

kjun.kr 2022. 4. 23. 23:01
728x90
728x170

1. Nuget 에서 Dapper 설치

2. 기본 사용

public class TestDataModel
{
    public int ID { get; set; }
    public string DATA1 { get; set; }
    public string DATA2 { get; set; }
    public string DATA3 { get; set; }
}
using Dapper;

using Prism.Commands;
using Prism.Mvvm;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows;
using System.Windows.Input;

private string connectionString = "Server=192.168.0.20;Database=kjundb;User Id=userid;Password=1234;";

using (IDbConnection db = new SqlConnection(this.connectionString))
{
    db.Open();

    // ■ Scalar
    var eventTime = db.Query<DateTime>("SELECT EventTime FROM TestTable WHERE ID = 1").SingleOrDefault();

    // ■ Basic Query
    var parameters = new { id = 18 };
    string sql = "SELECT * FROM TestTable WHERE ID = @id";

    var result = db.Query<TestDataModel>(sql, parameters);
}

 

728x90
그리드형