Simple use of micro ORM Framework--dapper

Source: Internet
Author: User

1. Installation

First use NuGet to install dapper, because the example here is to use MySQL, so also install the MySQL driver. Such as:

2 database table Scripts
SETForeign_key_checks=0;-- ------------------------------Table structure for class-- ----------------------------DROP TABLE IF EXISTS' class ';CREATE TABLE' class ' (' ID ' )int( One) not NULLauto_increment, ' name 'varchar(255)DEFAULT NULL,  PRIMARY KEY(' id ')) ENGINE=InnoDB auto_increment=4 DEFAULTCHARSET=UTF8;-- ------------------------------Table structure for student-- ----------------------------DROP TABLE IF EXISTS' student ';CREATE TABLE' student ' (' ID ')int( One) not NULLauto_increment, ' name 'varchar(255)DEFAULT NULL, ' age 'int( One)DEFAULT NULL, ' class_id 'int( One)DEFAULT NULL,  PRIMARY KEY(' id ')) ENGINE=InnoDB auto_increment=2001 DEFAULTCHARSET=UTF8;
3 Simple Example
Bulk inserts here are pseudo-batches, but in fact, multiple INSERT statements are generated. Interested can see the following blog implementation really BULK INSERT. Blog Address: http://www.cnblogs.com/renjing/p/MysqlBatchAdd.html
classProgram {Static stringConnStr ="Server=127.0.0.1;user id=root;password=root;database=renjing;"; Static voidMain (string[] args) {Stopwatch Watch=NewStopwatch (); Watch.            Start (); #region1. Insert//Bulk inserts here are pseudo-batches, but in fact, multiple INSERT statements are generated.            Interested can see the following blog implementation really BULK INSERT. //http://www.cnblogs.com/renjing/p/MysqlBatchAdd.html            #region1.0 BULK INSERT. Pseudo-Batch!//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //for (int i = 1; i <=; i++)//    {            //Conn. Execute ("INSERT into student (name,age,class_id) VALUES (@name, @age, @class_id)", new {name = "Xiaoming" + I, age = i% + 1, c            lass_id = i% 3 + 1}); //Console.WriteLine (i + ". Insert Success ...            "); //    }            //}            #endregion            #region1.2 BULK INSERT. Pseudo-Batch!//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //var dataList = enumerable.range (1, 1000).            Select (i = new {name= "Xiaoming" +i, age = i% + 1, class_id = i% 3 + 1}); //int count = conn.            Execute ("INSERT into student (name,age,class_id) VALUES (@name, @age, @class_id)", dataList); //Console.WriteLine ($ "Bulk Insert {count} Bar succeeded!!!            "); //}            #endregion            #endregion            #region2. Enquiry#region2.0 Basic Queries//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //var students = conn.            Query<student> ("select * from Student;"); //    //Parameterized Queries//    //var students = conn.            Query<student> ("select * from Student where [email protected];", new {name = "Xiaoming 13"}); //foreach (var item in students)//    {            //Console.WriteLine ($ "{item.id}\t{item.name}\t{item.age}"); //    }            //}            #endregion            #region2.1 Dynamic Type Query//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //var students = conn.            Query ("select * from Student;"); //foreach (var item in students)//    {            //Console.WriteLine ($ "{item.id}\t{item.name}\t{item.age}"); //    }            //}             #endregion            #region2.2 Multi-result query//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //var multi = conn.            Querymultiple ("SELECT * FROM Student;select * from class;"); //var studentlist = multi. Read<student> ().            ToList (); //var classlist = multi. Read<class> ().            ToList (); //Console.WriteLine ($ "Student:{studentlist.count} bar!            "); //Console.WriteLine ($ "Classlist:{classlist.count} bar!            "); //}            #endregion            #endregion            #region3. Modifications#region3.0 Modifications//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //var result = conn.            Execute ("Update student set [email protected] where [email protected];", new {name = "Xiao Ming elder brother", id = 100}); //Console.WriteLine ("3.1 revision succeeded"); //}             #endregion            #endregion            #region4. Delete#region4.1 Delete//using (mysqlconnection conn = new Mysqlconnection (connstr))//{            //var result = conn.            Execute ("Delete from student where [email protected];", new {id = 100}); //Console.WriteLine ("4.1 Delete succeeded"); //}            #endregion            #endregionWatch.            Stop (); Console.WriteLine (watch.            Elapsed);        Console.read (); }    }

Sample Source: Http://files.cnblogs.com/files/renjing/ORMTest.rar

Git address: Https://github.com/RichRen/dapper

Simple use of micro ORM Framework--dapper

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.