Dapper-is said to StackOverflow use the ORM

Source: Internet
Author: 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=.;D Atabase=dapper; Uid=sa;            pwd=******; ");            Book book = new book (); Book.            Name = "C # essence Theory";            string query = "INSERT into book (Name) VALUES (@name)"; Manipulate the object//conn.            Execute (query, book); Direct assignment Operation//conn.            Execute (query, new {name = "C # essence"});            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";            No parameter query, return list, with parameter query and previous parameter assignment method is the same. VAR l= Conn. Query<book> (query).            ToList ();            Returns a single message//string query = "SELECT * FROM book WHERE id = @id"; var book = conn. Query<book> (query, new {id = 8}).            Singleordefault (); When querying a book, find the corresponding book review at the same time, and there is a list. Implement 1--N query Operation//string "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) =//{////scan First Records, judging non-null and non-duplicates//if (lookup = = NULL | | lookup. Id! = Book.            ID)//lookup = Book;            Books corresponding to the book review is not empty, added to the current book review list, and finally the repetition of the book removed. if (Bookreview! = NULL)//lookup.      Reviews.add (Bookreview);      return lookup; }, new {id = 8}). Distinct ().            Singleordefault ();            1--1 Operation//bookreview BR; string query = "SELECT * from Bookreview BR left joins 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) {//start transaction 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);                Commit Transaction Transaction.commit (); } catch (Exception ex) {//exception occurred, transaction 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 () not NULL, [BOOKID] [int.] NOT NULL, [Content] [nvarchar] (a) 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 [Primar Y] Go Set ANSI_NULLS on Go Set QUOTED_IDENTIFIER on Go CREATE TABLE [dbo]. [Book] ([Id] [int] IDENTITY () not NULL, [Name] [nvarchar] (Ten) 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 # Essence theory 1 ') INSERT [dbo]. [Book] ([Id], [Name]) VALUES (9, N ' C # essentialism 4 ') SET identity_insert [dbo]. [Book] OFF */}

  

Dapper-is said to StackOverflow use the ORM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.