zoukankan      html  css  js  c++  java
  • Dapper常用方法总结

    常用方法

    #region Dapper 数据库操作
    
    
            #region Query 操作
    
            /// <summary>
            /// 无参获取单个实体modle
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns>T</returns>
            public static T QueryModel<T>(string sqlstr) where T : new()
            {
    
                T t = new T();
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
    
                    try
                    {
                        connection.Open();
                        t = connection.QueryFirstOrDefault<T>(sqlstr);
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        //throw new Exception(ex.Message);
                        connection.Close();
                    }
                }
    
                return t;
            }
    
    
            /// <summary>
            /// 参数化获取单个实体modle 
            /// </summary>
            /// <param name="sqlstr">执行sql语句</param>
            /// <param name="parms">参数数组</param>
            /// <returns>T</returns>
            public static T QueryModel<T>(string sqlstr, params MySqlParameter[] parms) where T : new()
            {
    
                T t = new T();
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
    
                    try
                    {
                        connection.Open();
                        var args = new DynamicParameters(new { });
                        foreach (MySqlParameter parm in parms)
                        {
                            args.Add(parm.ParameterName,parm.Value);
                        }
                        t = connection.QueryFirstOrDefault<T>(sqlstr,args);
                                       
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        //throw new Exception(ex.Message);
                        connection.Close();
                    }
                }
    
                return t;
            }
    
            /// <summary>
            /// 获取多个实体modle
            /// </summary>
            /// <param name="sqlString">查询语句</param>
            /// <returns>List<T></returns>
            public static List<T> QueryModelList<T>(string sqlString) where T : new()
            {
    
                List<T> tlist = new List<T>();
                using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
                {
    
                    try
                    {
                        connection.Open();
                        tlist = connection.Query<T>(sqlString).ToList();
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        //throw new Exception(ex.Message);
                        connection.Close();
                    }
                    return tlist;
                }
            }
    
            /// <summary>
            ///  参数化获取多个实体modle
            /// </summary>
            /// <param name="sqlString">查询语句</param>
            /// <returns>List<T></returns>
            public static List<T> QueryModelList<T>(string sqlString, params MySqlParameter[] parms) where T : new()
            {
    
                List<T> tlist = new List<T>();
                using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
                {
    
                    try
                    {
                        connection.Open();
                        var args = new DynamicParameters(new { });
                        foreach (MySqlParameter parm in parms)
                        {
                            args.Add(parm.ParameterName, parm.Value);
                        }
                        tlist = connection.Query<T>(sqlString, args).ToList();
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        //throw new Exception(ex.Message);
                        connection.Close();
                    }
                    return tlist;
                }
            }
    
    
            #endregion
    
    
            #region Execute  操作
    
            /// <summary>
            /// 单条执行一条sql数据
            /// </summary>
            /// <param name="sqlstr">执行语句</param>
            /// <returns></returns>
            public static int Execute(string sqlstr)
            {
                int obj = 0;
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
    
                    try
                    {
                        connection.Open();
                        obj = connection.Execute(sqlstr);
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        //throw new Exception(ex.Message);
                        connection.Close();
    
                    }
                }
    
                return obj;
            }
    
    
            /// <summary>
            /// 参数化单条执行一条sql数据
            /// </summary>
            /// <param name="sqlstr">执行语句</param>
            /// <returns></returns>
            public static int Execute(string sqlstr, params MySqlParameter[] parms)
            {
                int obj = 0;
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
    
                    try
                    {
                        connection.Open();
                        var args = new DynamicParameters(new { });
                        foreach (MySqlParameter parm in parms)
                        {
                            args.Add(parm.ParameterName, parm.Value);
                        }
                        obj = connection.Execute(sqlstr, args);
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        //throw new Exception(ex.Message);
                        connection.Close();
    
                    }
                }
    
                return obj;
            }
    
    
    
    
    
    
            #endregion
    
    
    
    
            #endregion
  • 相关阅读:
    Nginx 日志切割-定时(附数据库数据备份)
    安装Nginx
    系统自适应限流
    黑名名单控制-sentinel
    热点参数的流量控制
    流量控制文档说明
    在Linux中输入命令时打错并按了enter
    配置maven环境
    项目层次展示
    寻找cmd的管理员运行
  • 原文地址:https://www.cnblogs.com/for917157ever/p/14169898.html
Copyright © 2011-2022 走看看