dapper 增删改查

连接对象

using Simple.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Simple.Data.Mysql;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;

namespace histest.Tools
{
    public class DataBaseHelper
    {
        private static readonly string ConnectionSqlserver = ConfigurationManager.ConnectionStrings["dbsqlserver"].ConnectionString;
        private static readonly string ConnectionMysql = ConfigurationManager.ConnectionStrings["dbmysql"].ConnectionString;

        //dapper
        public static SqlConnection DepperSqlserver()
        {
            SqlConnection connection = new SqlConnection(ConnectionSqlserver);
            connection.Open();
            return connection;
        }

        public static MySqlConnection DepperMysql()
        {
            MySqlConnection connection = new MySqlConnection(ConnectionMysql);
            connection.Open();
            return connection;
        }
    }
}



增删改查
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Dapper;
using System.Collections;

namespace histest.Tools
{
    public class DapperHelper
    {

        public static List<T> Select<T>(string sql, Object param)
        {
            using (IDbConnection conn = DataBaseHelper.DepperSqlserver())
            {
                List<T> datas =  conn.Query<T>(sql, param).ToList<T>();
                return datas;
            }
        }

        public static T Find<T>(string sql, Object param)
        {
            using (IDbConnection conn = DataBaseHelper.DepperSqlserver()) {
                T datas = conn.Query<T>(sql, param).SingleOrDefault<T>();
                return datas;
            }
        }

        public static int Count(string sql,Object param)
        {
            using (IDbConnection conn = DataBaseHelper.DepperSqlserver()) {
                List<dynamic> datas = conn.Query(sql, param).ToList();
                return datas.ToArray().Length;
            }
        }

        public static int Update(string sql,Object param)
        {
            using (IDbConnection conn = DataBaseHelper.DepperSqlserver())
            {
                return conn.Execute(sql,param);
            }            
        }

        public static int Add(string sql,Object param)
        {
            using (IDbConnection conn = DataBaseHelper.DepperSqlserver()) {
                 return conn.Execute(sql, param);
            }           
        }

        public static int Execute(String sql,Object param)
        {
            using (IDbConnection conn = DataBaseHelper.DepperSqlserver())
            {
                return conn.Execute(sql, param);
            } 
        }

    }
}


使用

查询一条记录 DapperHelper.Find<dynamic>("select * from where id=@id",new{id=7} );
查询多条记录 DapperHelper.Select<dynamic>("select * from where typeid=@typeid",new{typeid=5} );
查询总条数  DapperHelper.Count<dynamic>("select * from where typeid=@typeid",new{typeid=5} );
增删改 DapperHelper.Execute(sql, new{username="zhangsan",password="admin"});



转载于:https://my.oschina.net/tongjh/blog/547445

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值