Dapper-据说stackoverflow使用的orm

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace DapperTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IDbConnection conn = new SqlConnection("Server=.;Database=Dapper;Uid=sa;Pwd=******;");

            //Book book = new Book();
            //book.Name = "C#本质论";
            //string query = "INSERT INTO Book(Name)VALUES(@name)";
            ////对对象进行操作
            //conn.Execute(query, book);
            ////直接赋值操作
            //conn.Execute(query, new { name = "C#本质论" });


            //string query = "UPDATE Book SET  Name=@name WHERE id =@id";
            //conn.Execute(query, new{name="C# VS Java",id=3});

            //Book book = new Book() { Id = 3 };
            //string query = "DELETE FROM Book WHERE id = @id";
            //conn.Execute(query, book);
            //conn.Execute(query, new { id = 5 });

            //string query = "SELECT * FROM Book";
            ////无参数查询,返回列表,带参数查询和之前的参数赋值法相同。
            //var l= conn.Query<Book>(query).ToList();

            //返回单条信息
            //string query = "SELECT * FROM Book WHERE id = @id";
            //var book = conn.Query<Book>(query, new { id = 8 }).SingleOrDefault();


            //查询图书时,同时查找对应的书评,并存在List中。实现1--n的查询操作
            //string query = "SELECT * FROM Book b LEFT JOIN BookReview br ON br.BookId = b.Id WHERE b.id = @id";
            //Book lookup = null;
            ////Query<TFirst, TSecond, TReturn>
            //var b = conn.Query<Book, BookReview, Book>(query,
            // (book, bookReview) =>
            // {
            //     //扫描第一条记录,判断非空和非重复
            //     if (lookup == null || lookup.Id != book.Id)
            //         lookup = book;
            //     //书对应的书评非空,加入当前书的书评List中,最后把重复的书去掉。
            //     if (bookReview != null)
            //         lookup.Reviews.Add(bookReview);
            //     return lookup;
            // }, new { id = 8 }).Distinct().SingleOrDefault();

            //1--1操作 
            //BookReview br;
            //string query = "SELECT * FROM BookReview br LEFT JOIN Book b ON br.BookId = b.Id WHERE br.id = @id";
            //using (conn)
            //{
            //    br = conn.Query<BookReview, Book, BookReview>(query,
            //   (bookReview, book) =>
            //   {
            //       bookReview.AssoicationWithBook = book;
            //       return bookReview;
            //   }, new { id = 4 }).SingleOrDefault();

            //}


            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            using (conn)
            {
                //开始事务
                IDbTransaction transaction = conn.BeginTransaction();
                try
                {
                    string query = "delete from book where id = @id";
                    string query2 = "delete from BookReview where BookId = @BookId";
                    conn.Execute(query2, new { BookId = 7 }, transaction, null, null);
                    conn.Execute(query, new { id = 7 }, transaction, null, null);
                    //提交事务
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    //出现异常,事务Rollback
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }

            Console.WriteLine("Ok");
            Console.ReadKey();
        }
    }

    public class Book
    {
        public Book()
        {
            Reviews = new List<BookReview>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual List<BookReview> Reviews { get; set; }
        public override string ToString()
        {
            return string.Format("[{0}]------《{1}》", Id, Name);
        }
    }

    public class BookReview
    {
        public int Id { get; set; }
        public int BookId { get; set; }
        public virtual string Content { get; set; }
        public virtual Book AssoicationWithBook { get; set; }
        public override string ToString()
        {
            return string.Format("{0})--[{1}]	"{3}"", Id, BookId, Content);
        }
    }

    /*
     * USE [Dapper]
     * 
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        CREATE TABLE [dbo].[BookReview](
	        [Id] [int] IDENTITY(1,1) NOT NULL,
	        [BookId] [int] NOT NULL,
	        [Content] [nvarchar](500) NOT NULL,
         CONSTRAINT [PK_BookReview] PRIMARY KEY CLUSTERED 
        (
	        [Id] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]
        GO
 
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        CREATE TABLE [dbo].[Book](
	        [Id] [int] IDENTITY(1,1) NOT NULL,
	        [Name] [nvarchar](10) NOT NULL,
         CONSTRAINT [PK_Book] PRIMARY KEY CLUSTERED 
        (
	        [Id] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]
        GO
        SET IDENTITY_INSERT [dbo].[Book] ON
        INSERT [dbo].[Book] ([Id], [Name]) VALUES (6, N'C#本质论1')
        INSERT [dbo].[Book] ([Id], [Name]) VALUES (9, N'C#本质论4')
        SET IDENTITY_INSERT [dbo].[Book] OFF

     */
}

  

原文地址:https://www.cnblogs.com/ICE_Inspire/p/5553657.html