Micro-orm framework-simple use of dapper and orm framework-dapper

Source: Internet
Author: User

Micro-orm framework-simple use of dapper and orm framework-dapper
1. Install

First, use nuget to install dapper. Because the example here uses mysql, you must install the mysql driver. For example:

2. Database Table scripts
SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for class-- ----------------------------DROP TABLE IF EXISTS `class`;CREATE TABLE `class` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(255) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;-- ------------------------------ Table structure for student-- ----------------------------DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(255) DEFAULT NULL,  `age` int(11) DEFAULT NULL,  `class_id` int(11) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2001 DEFAULT CHARSET=utf8;
3. Simple Example
Batch inserts are all pseudo-Batch inserts. In fact, multiple insert statements are generated. If you are interested, refer to the following blog to implement batch insertion. Blog: http://www.cnblogs.com/renjing/p/MysqlBatchAdd.html
Class Program {static string connStr = "server = 127.0.0.1; user id = root; password = root; database = renjing;"; static void Main (string [] args) {Stopwatch watch = new Stopwatch (); watch. start (); # region 1. insert // The batch inserts here are all pseudo-Batch inserts. In fact, multiple insert statements are generated. If you are interested, refer to the following blog to implement batch insertion. // http://www.cnblogs.com/renjing/p/MysqlBatchAdd.html # Region 1.0 batch insert. Pseudo batch! // Using (MySqlConnection conn = new MySqlConnection (connStr) // {// for (int I = 1; I <= 1000; I ++) // {// conn. execute ("insert into student (name, age, class_id) values (@ name, @ age, @ class_id)", new {name = "James" + I, age = I % 100 + 1, class_id = I % 3 + 1}); // Console. writeLine (I + ". inserted successfully... "); //} # Endregion # region 1.2 batch insert. Pseudo batch! // Using (MySqlConnection conn = new MySqlConnection (connStr) // {// var dataList = Enumerable. range (1, 1000 ). select (I => new {name = "James" + I, age = I % 100 + 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 ($ "batch insert {count} records succeeded !!! "); //} # Endregion # region 2. query # region 2.0 basic query // using (MySqlConnection conn = new MySqlConnection (connStr) // {// var students = conn. query <Student> ("select * from student;"); // parameterized Query // var students = conn. query <Student> ("select * from student where name = @ name;", new {name = "James 13"}); // foreach (var item in students) // {// Console. writeLine ($ "{item. id} \ t {item. name} \ t {item. Age} "); //} # endregion # region 2.1 dynamic 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 # region 2.2 multi-result query // using (MySqlConnection conn = new MySqlConnection (connStr )) // {// var multi = conn. queryM Ultiple ("select * from student; select * from class;"); // var studentList = multi. read <Student> (). toList (); // var classList = multi. read <Class> (). toList (); // Console. writeLine ($ "student: {studentList. count! "); // Console. WriteLine ($" classList: {classList. Count! "); //} # Endregion # region 3. modify # region 3.0 modify // using (MySqlConnection conn = new MySqlConnection (connStr) // {// var result = conn. execute ("update student set name = @ name where id = @ id;", new {name = "James brother", id = 100}); // Console. writeLine ("3.1 modified successfully"); //} # endregion # region 4. delete # region 4.1 Delete // using (MySqlConnection conn = new MySqlConnection (connStr) // {// var result = conn. execute ("delete from student where id = @ id;", new {id = 100}); // Console. writeLine ("4.1 deleted successfully"); //} # endregion watch. stop (); Console. writeLine (watch. elapsed); Console. read ();}}

Example source code: http://files.cnblogs.com/files/renjing/ORMTest.rar

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

 

Related Article

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.