Orm-dapper Learning < four;. Basic usage of Dapper

Source: Internet
Author: User

1. New
 Public intinsert_user_info (User_info_model user) {using(conn) {stringquery =@"INSERT into User_info (name, pwd, logindate) VALUES (@name, @pwd, @loginDate)"; introw =Conn.                Execute (query, user); //The ID of the updated object is the new ID in the database, and if you add it, you don't need to get the new object .//simply add the object to the database, and you can comment out the following line.                 intmax_id = setidentity (conn, id = user.id = ID,"ID","User_info"); returnRow; }} Note 1: Additional Setidentity method (. net3.5 and below):/// <param name= "conn" >IDbConnection</param>/// <param name= "SetId" >ActionId</param>/// <param name= "PrimaryKey" >PRIMARY Key</param>/// <param name= "TableName" >Table name</param> Public intSetidentity (IDbConnection conn, action<int> SetId,stringPrimaryKeystringtableName) {            if(string. IsNullOrEmpty (PrimaryKey)) PrimaryKey ="ID"; if(string. IsNullOrEmpty (TableName)) {Throw NewArgumentException ("The tablename parameter cannot be null for the queried table name"); }            stringquery =string. Format ("SELECT max ({0}) as ID from {1}", PrimaryKey, tableName); NewId Identity= Conn. Query<newid> (Query,NULL).            Single (); SetId (identity.            ID); returnidentity.        Id; Note 2: Implement setidentity with Net4.0 's new dynamic type (this method is recommended) Public intSetidentity<user_info> (User_info_model user, IDbConnection conn, action<int>setId) {            Dynamicidentity = conn. Query ("INSERT into User_info (name, pwd, logindate)VALUES (@name, @pwd, @loginDate); SELECT @ @IDENTITY as Id"). Single ();NewId ID =(NewId) identity.            Id; SetId (ID.            ID); returnID.        Id; }

2. Update
 Public int update_user_info_by_id (intstring  name)        {            using  (conn            {                string@"";                 return New {ID, name});            }        }

3. Delete
 Public int delete_user_info_by_id (int  ID)        {            using  (conn)            {                 string @" ";                 return New {ID});            }        }
4. Enquiry
 Public Ienumerable<user_info_model> get_user_info ()        {            using  (conn)            {                 string @" ";                 return Conn. query<user_info_model>(query);            }        }
5. Reference and SQL Statement keyword Usage
 public  user_info_model get_user_info_by_id ( int  ID, string          name)            {User_info_model user;  using   (conn) { string  query =  "  SELECT * from User_info WHERE ID [email protected] and name like @name   "                Span style= "COLOR: #000000" >; User  = conn. Query<user_info_model> (query, new   {ID, Name}).                FirstOrDefault ();             return   user; }        }

Note: When the parameter name passed in is the same as in the SQL statement, you do not need to specify a parameter (for example, above), but instead specify the parameter (for example, below):

Parameters:intstringnew {id = id1, name = name1}). Singleordefault ();

Orm-dapper Learning < four;. Basic usage of 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.