CSharp · Console
// Learn Dapper
// Doc: https://www.learndapper.com/execute-reader
// @nuget: Dapper
// @nuget: Microsoft.Data.SqlClient
// @nuget: Z.Dapper.Plus
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.Data.SqlClient;
using Dapper;
using Z.Dapper.Plus;
public class Program
{
public static void Main()
{
var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
// CREATE database and data seeding
{
// CREATE Table (from Dapper Plus)
connection.CreateTable<Product>();
}
// Insert data with Dapper
var sql1 = "INSERT INTO Product (CategoryID, Name, Description) VALUES (@categoryID, @name, @description)";
object[] parameters = { new { categoryID = 1, name = "Dapper Plus", description = @"Use <a href=""https://dapper-plus.net/"" target=""_blank"">Dapper Plus</a> to extend your IDbConnection with high-performance bulk operations." }};
connection.Execute(sql1, parameters);
// Query data with Dapper
var sql2 = "SELECT * FROM Product WHERE CategoryID = @categoryID";
var products = connection.Query<Product>(sql2, new { categoryID = 1 }).ToList();
FiddleHelper.WriteTable(products);
}
[Table("Product")]
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public int CategoryID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}