Dapper to use, instead of your dbsqlhelper

Source: Internet
Author: User

Dapper Introduction

Dapper is a lightweight ORM framework under. NET. With a small tool-to-project, using dapper will make the database operation layer code more elegant.

use of Dapper

Using reference dapper in your project is very simple and you can choose between two ways:

1. References in NuGet

2.Github Download the source code and add it to the project.

Dapper Project Address Https://github.com/StackExchange/Dapper

Add Project Dapper to your own project, so you can debug Dapper source code directly

Personal feeling Add source code to the project is more reliable, encountered problems can be directly traced

Dapper Project

Add dapper source code to project

curd Operation

Table structure

1 CREATE TABLE [dbo].[Bas_name](2     [ID] [INT] NULL,3     [NAME] [VARCHAR]( -)NULL,4     [ Other] [VARCHAR]( -)NULL,5     [DATE] [DATETIME] NULL6) on [PRIMARY]
1.Insert Operation

As you see in the code, it is easier to use execute plus entities directly than the traditional SqlParameter approach.

1IDbConnection conn =NewSqlConnection (connectionString);2 //Insert3 stringInsetsql ="INSERT dbo. Bas_name (ID, NAME, Other, DATE) VALUES (@ID, @NAME, @OTHER, @DATE)";4Bas_name Nameinfo =Newbas_name ();5Nameinfo.id =0;6Nameinfo.name ="Xiao Wang";7Nameinfo.other =string. Empty;8Nameinfo.date =DateTime.Now;9  var result = Conn. Execute (Insetsql, nameinfo); Ten  One Console.Write (result); AConsole.readkey ();

Of course, you can also use this method to add rows

1 var 2     New 1 " Xiao Li ", other="233", DATE = DateTime.Now});

2.UPDATE Operation

Same as Insert

1Bas_name Nameinfo =Newbas_name ();2Nameinfo.id =0;3Nameinfo.name ="Xiao Wang 233";4Nameinfo.other ="233";5Nameinfo.date =DateTime.Now;6 7 stringUpdatesql ="UPDATE dbo. Bas_name SET NAME = @NAME, other= @OTHER, [DATE] = @DATE WHERE ID = @ID";8 9 varresult = Conn. Execute (Updatesql, nameinfo);

3.Select operation
string " SELECT * from Bas_name " ; List<BAS_NAME> basnamelist = conn. query<bas_name>"select * from bas_name WHERE ID = @ID"= conn. Query<bas_name> (Selectsql,new1 }). ToList ();

4.Delete operation
var result = Conn. Execute ("DELETE from dbo. Bas_name WHERE ID = @ID"new0 }";

Dapper to use, instead of your dbsqlhelper

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.