Dapper-據說stackoverflow使用的orm

來源:互聯網
上載者:User

標籤:

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  [email protected] WHERE id [email protected]";            //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     */}

  

Dapper-據說stackoverflow使用的orm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.