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