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}]\t\"{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 */ }