/// <summary>
 /// 获取DataSet
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars"></param>
 /// <returns></returns>
 public virtual DataSet GetDataSetAll(string sql, object pars)
 {
     return(GetDataSetAll(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #2
0
        /// <summary>
        /// 获取表结构信息
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List <PubModel.DataTableMap> GetTableColumns(SqlSugarClient db, string tableName)
        {
            string sql = SqlSugarTool.GetTtableColumnsInfo(tableName);

            return(db.SqlQuery <PubModel.DataTableMap>(sql));
        }
 /// <summary>
 /// 根据SQL获取T
 /// </summary>
 /// <typeparam name="T">可以是int、string等,也可以是类或者数组、字典</typeparam>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public virtual T GetSingle <T>(string sql, object pars)
 {
     return(GetSingle <T>(sql, SqlSugarTool.GetParameters(pars)));
 }
        /// <summary>
        /// 根据SQL获取T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public virtual T GetSingle <T>(string sql, params MySqlParameter[] pars)
        {
            var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), GetReader(sql, pars), null).Single();

            return(reval);
        }
 /// <summary>
 /// 获取DataReader
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public virtual MySqlDataReader GetReader(string sql, object pars)
 {
     return(GetReader(sql, SqlSugarTool.GetParameters(pars)));
 }
 /// <summary>
 /// 根据SQL获取T的集合
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public virtual List <T> GetList <T>(string sql, object pars)
 {
     return(GetList <T>(sql, SqlSugarTool.GetParameters(pars)));
 }
 /// <summary>
 /// 获取第一行第一列
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public virtual object GetScalar(string sql, object pars)
 {
     return(GetScalar(sql, SqlSugarTool.GetParameters(pars)));
 }
 /// <summary>
 /// 执行SQL返回受影响行数
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public virtual int ExecuteCommand(string sql, object pars)
 {
     return(ExecuteCommand(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #9
0
 /// <summary>
 /// 获取DataTable
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public DataTable GetDataTable(string sql, object pars)
 {
     return(GetDataTable(sql, SqlSugarTool.GetParameters(pars)));
 }
 /// <summary>
 /// 获取第一行第一列
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public virtual int GetInt(string sql, object pars)
 {
     return(GetInt(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #11
0
        /// <summary>
        /// 根据SQL获取T的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public List <T> GetList <T>(string sql, params MySqlParameter[] pars)
        {
            var reval = SqlSugarTool.DataReaderToList <T>(typeof(T), GetReader(sql, pars), null);

            return(reval);
        }
예제 #12
0
 /// <summary>
 /// 获取第一行第一列
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param>
 /// <returns></returns>
 public string GetString(string sql, object pars)
 {
     return(GetString(sql, SqlSugarTool.GetParameters(pars)));
 }
        /// <summary>
        /// 动态获取IDataRecord里面的函数
        /// </summary>
        /// <param name="generator"></param>
        /// <param name="type"></param>
        /// <param name="isNullable"></param>
        /// <param name="pro"></param>
        /// <param name="dbTypeName"></param>
        /// <param name="fieldName"></param>
        private static void GeneratorCallMethod(ILGenerator generator, Type type, bool isNullable, PropertyInfo pro, string dbTypeName, string fieldName)
        {
            List <string> guidThrow = new List <string>()
            {
                "int32", "datetime", "decimal", "double", "byte", "string"
            };                                                                                                         //数据库为GUID有错的实体类形
            List <string> intThrow = new List <string>()
            {
                "datetime", "byte"
            };                                                                //数据库为int有错的实体类形
            List <string> stringThrow = new List <string>()
            {
                "int32", "datetime", "decimal", "double", "byte", "guid"
            };                                                                                                         //数据库为vachar有错的实体类形
            List <string> decimalThrow = new List <string>()
            {
                "datetime", "byte", "guid"
            };
            List <string> doubleThrow = new List <string>()
            {
                "datetime", "byte", "guid"
            };
            List <string> dateThrow = new List <string>()
            {
                "int32", "decimal", "double", "byte", "guid"
            };
            List <string> shortThrow = new List <string>()
            {
                "datetime", "guid"
            };
            MethodInfo method      = null;
            var        typeName    = SqlSugarTool.ChangeDBTypeToCSharpType(dbTypeName);
            var        objTypeName = type.Name.ToLower();
            var        isEnum      = type.IsEnum;

            if (isEnum)
            {
                typeName = "ENUMNAME";
            }
            else if (typeName.IsIn("byte[]", "other"))
            {
                generator.Emit(OpCodes.Call, getValueMethod);
                generator.Emit(OpCodes.Unbox_Any, pro.PropertyType);//找不到类型才执行拆箱(类型转换)
                return;
            }
            if (isNullable)
            {
                switch (typeName)
                {
                case "int":
                    CheckType(intThrow, objTypeName, typeName, fieldName);
                    var isNotInt = objTypeName != "int32";
                    if (isNotInt)
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertInt32;
                    } break;

                case "bool":
                    if (objTypeName != "bool" && objTypeName != "boolean")
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertBoolean;
                    } break;

                case "string":
                    CheckType(stringThrow, objTypeName, typeName, fieldName);
                    method = getString; break;

                case "dateTime":
                    CheckType(dateThrow, objTypeName, typeName, fieldName);
                    if (objTypeName != "datetime")
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertDateTime;
                    } break;

                case "decimal":
                    CheckType(decimalThrow, objTypeName, typeName, fieldName);
                    var isNotDecimal = objTypeName != "decimal";
                    if (isNotDecimal)
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertDecimal;
                    } break;

                case "double":
                    CheckType(doubleThrow, objTypeName, typeName, fieldName);
                    var isNotDouble = objTypeName != "double";
                    if (isNotDouble)
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertDouble;
                    } break;

                case "guid":
                    CheckType(guidThrow, objTypeName, typeName, fieldName);
                    if (objTypeName != "guid")
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertGuid;
                    } break;

                case "byte":
                    method = getConvertByte; break;

                case "ENUMNAME":
                    method = getConvertToEnum_Nullable.MakeGenericMethod(type); break;

                case "short":
                    CheckType(shortThrow, objTypeName, typeName, fieldName);
                    var isNotShort = objTypeName != "int16" && objTypeName != "short";
                    if (isNotShort)
                    {
                        method = getOtherNull.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getConvertInt16;
                    }
                    break;

                default:
                    method = getOtherNull.MakeGenericMethod(type); break;
                }

                generator.Emit(OpCodes.Call, method);
            }
            else
            {
                switch (typeName)
                {
                case "int":
                    CheckType(intThrow, objTypeName, typeName, fieldName);
                    var isNotInt = objTypeName != "int32";
                    if (isNotInt)
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getInt32;
                    } break;

                case "bool":
                    if (objTypeName != "bool" && objTypeName != "boolean")
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getBoolean;
                    } break;

                case "string":
                    CheckType(stringThrow, objTypeName, typeName, fieldName);
                    method = getString; break;

                case "dateTime":
                    CheckType(dateThrow, objTypeName, typeName, fieldName);
                    if (objTypeName != "datetime")
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getDateTime;
                    } break;

                case "decimal":
                    CheckType(decimalThrow, objTypeName, typeName, fieldName);
                    var isNotDecimal = objTypeName != "decimal";
                    if (isNotDecimal)
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getDecimal;
                    } break;

                case "double":
                    CheckType(doubleThrow, objTypeName, typeName, fieldName);
                    var isNotDouble = objTypeName != "double";
                    if (isNotDouble)
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getDouble;
                    } break;

                case "guid":
                    CheckType(guidThrow, objTypeName, typeName, fieldName);
                    if (objTypeName != "guid")
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getGuid;
                    } break;

                case "byte":
                    method = getByte; break;

                case "ENUMNAME":
                    method = getValueMethod; break;

                case "short":
                    CheckType(shortThrow, objTypeName, typeName, fieldName);
                    var isNotShort = objTypeName != "int16" && objTypeName != "short";
                    if (isNotShort)
                    {
                        method = getOther.MakeGenericMethod(type);
                    }
                    else
                    {
                        method = getInt16;
                    }
                    break;

                default: method = getOther.MakeGenericMethod(type); break;;
                }

                generator.Emit(OpCodes.Call, method);

                if (method == getValueMethod)
                {
                    generator.Emit(OpCodes.Unbox_Any, pro.PropertyType);//找不到类型才执行拆箱(类型转换)
                }
            }
        }
예제 #14
0
 /// <summary>
 ///获取没有符号的参数名称
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 internal static string GetSqlParameterNameNoParSymbol(this string name)
 {
     return(SqlSugarTool.GetSqlParameterNameNoParSymbol(name));
 }
예제 #15
0
 /// <summary>
 /// 获取转释后的表名和列名
 /// </summary>
 /// <param name="tableName"></param>
 /// <returns></returns>
 public static string GetTranslationSqlName(this string tableName)
 {
     return(SqlSugarTool.GetTranslationSqlName(tableName));
 }