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
  • 相关阅读:
    想学数据库的进来领课程了哈.....
    天轰穿C# vs2010 04面向对象的编程之密封【原创】
    天轰穿C#vs2010 04面向对象的编程之访问数组 【原创】
    天轰穿C#vs201004面向对象的编程之foreach循环【原创】
    天轰穿C# vs2010 04面向对象的编程之继承【原创】
    【IT职业规划】天轰穿教你如何选择和学习编程
    天轰穿C# vs2010 04面向对象的编程之简单数组 【原创】
    关闭任务计划程序前您必须关闭所有会话框的解决方法
    用树莓派实现会说话的汤姆猫
    备忘录:关于.net程序连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/for917157ever/p/14169898.html
Copyright © 2011-2022 走看看