首页 > 代码库 > ORM-Dapper学习<四>.Dapper的基本用法

ORM-Dapper学习<四>.Dapper的基本用法

1.    新增

public int insert_user_info(user_info_model user)
        {
            using (conn)
            {
                string query = @"INSERT INTO user_info (name ,pwd ,loginDate) 
                                 VALUES (@name,@pwd,@loginDate) ";
                int row = conn.Execute(query, user);
                //更新对象的Id为数据库里新增的Id,假如增加之后不需要获得新增的对象,
                //只需将对象添加到数据库里,可以将下面的一行注释掉。
                int max_id = SetIdentity(conn, id => user.id = id, "id", "user_info");
                return row;
            }
        }
注1:附加SetIdentity方法(.net3.5及以下):
/// <param name="conn">IDbConnection</param>
/// <param name="setId">ActionID</param>
/// <param name="primarykey">主键</param>
/// <param name="tableName">表名</param>
public int SetIdentity(IDbConnection conn, Action<int> setId, string primarykey, string tableName)
        {
            if (string.IsNullOrEmpty(primarykey)) primarykey = "id";
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("tableName参数不能为空,为查询的表名");
            }
            string query = string.Format("SELECT max({0}) as id FROM {1}", primarykey, tableName);
            NewId identity = conn.Query<NewId>(query, null).Single();
            setId(identity.Id);
            return identity.Id;
        }





注2:借助Net4.0新增的dynamic动态类型实现SetIdentity(建议使用此方法)
public int SetIdentity<user_info>( user_info_model user, IDbConnection conn, Action<int> setId)
        {
            dynamic identity = 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);
            return id.Id;
        }

 

2.    更新

public int update_user_info_by_id(int id, string name)
        {
            using (conn)
            {
                string query = @"Update user_info SET name = @name WHERE id = @id ";
                return conn.Execute(query, new { id, name });
            }
        }

 

3.    删除

public int delete_user_info_by_id(int id)
        {
            using (conn)
            {
                string query = @"DELETE FROM user_info WHERE id = @id ";
                return conn.Execute(query, new { id });
            }
        }

4.    查询

public IEnumerable<user_info_model> get_user_info()
        {
            using (conn)
            {
                string query = @"SELECT * FROM user_info ";
                return conn.Query<user_info_model>(query);
            }
        }

5.    传参及SQL语句关键字用法

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 =@id AND name like @name ";
                user = conn.Query<user_info_model>(query, new { id, name }).FirstOrDefault();
                return user;
            }
        }

注一:当传进来的参数名称与sql语句中的相同时,不需指定参数(示例如上),反之需要指定参数(示例如下):

参数:int id1, string name1
Sql语句:同上示例
user = conn.Query<user_info_model>(query, new { id = id1, name = name1 }).SingleOrDefault();

 

ORM-Dapper学习<四>.Dapper的基本用法