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