Beispiel #1
0
        /// <summary>
        /// 条件比较
        /// </summary>
        private static bool GetTFilterOk(string where, int quoteIndex, string sign, Op op, MDataColumn mdc, out TFilter tFilter)
        {
            bool result = false;

            tFilter = null;
            int index = where.ToLower().IndexOf(sign, 0, quoteIndex > 0 ? quoteIndex : where.Length);

            if (index > 0)
            {
                string columnAName  = where.Substring(0, index).Trim();
                int    columnAIndex = mdc.GetIndex(columnAName);
                string valueB       = where.Substring(index + sign.Length).Trim(' ', '\'').Replace("''", "'");
                if (op == Op.In || op == Op.NotIn)
                {
                    valueB = ',' + valueB.TrimStart('(', ')').Replace("'", "") + ",";//去除单引号。
                }
                int columnBIndex = -1;
                if (quoteIndex == 0 && mdc.Contains(valueB)) //判断右侧的是否列名。
                {
                    columnBIndex = mdc.GetIndex(valueB);
                }
                tFilter = new TFilter(Ao.None, columnAName, columnAIndex, op, valueB, columnBIndex);
                if (columnBIndex == -1 && !string.IsNullOrEmpty(Convert.ToString(valueB)))      //右侧是值类型,转换值的类型。
                {
                    if (columnAIndex > -1 && DataType.GetGroup(mdc[columnAIndex].SqlType) == 3) //bool型
                    {
                        switch (Convert.ToString(tFilter._valueB).ToLower())
                        {
                        case "true":
                        case "1":
                        case "on":
                            tFilter._valueB = true;
                            break;

                        case "false":
                        case "0":
                        case "":
                            tFilter._valueB = false;
                            break;

                        default:
                            tFilter._valueB = null;
                            break;
                        }
                    }
                    else
                    {
                        try
                        {
                            tFilter._valueB = ConvertTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType);
                        }
                        catch
                        {
                        }
                    }
                }
                result = true;
            }
            return(result);
        }
Beispiel #2
0
 internal T Get <T>()
 {
     if (CellValue.IsNull)
     {
         return(default(T));
     }
     return(ConvertTool.ChangeType <T>(Value));
     //if (isNewValue)
     //{
     //    Type t = typeof(T);
     //    return (T)ChangeValue(CellValue.SourceValue, t, DataType.GetGroup(DataType.GetSqlType(t)));
     //}
     // return (T)Value;
 }
Beispiel #3
0
        /// <summary>
        ///  值的数据类型转换。
        /// </summary>
        /// <param name="value">要被转换的值</param>
        /// <param name="convertionType">要转换成哪种类型</param>
        /// <param name="groupID">数据库类型的组号</param>
        /// <returns></returns>
        internal object ChangeValue(object value, Type convertionType, int groupID)
        {
            //值不为null
            try
            {
                switch (groupID)
                {
                case 0:
                    if (_CellStruct.SqlType == SqlDbType.Time)    //time类型的特殊处理。
                    {
                        string[] items = StringValue.Split(' ');
                        if (items.Length > 1)
                        {
                            CellValue.StringValue = items[1];
                        }
                    }
                    value = StringValue;
                    break;

                default:
                    value = ConvertTool.ChangeType(value, convertionType);
                    //if (convertionType.Name.EndsWith("[]"))
                    //{
                    //    value = Convert.FromBase64String(StringValue);
                    //    CellValue.StringValue = "System.Byte[]";
                    //}
                    //else
                    //{

                    //}
                    break;
                }
            }
            catch (Exception err)
            {
                CellValue.Value       = null;
                CellValue.IsNull      = true;
                CellValue.StringValue = null;
                isNewValue            = false;
                string msg = string.Format("ChangeType Error:ColumnName【{0}】({1}) , Value:【{2}】\r\n", _CellStruct.ColumnName, _CellStruct.ValueType.FullName, StringValue);

                Log.Write(msg, LogType.Error);
                return(null);
            }
            return(value);
        }
Beispiel #4
0
        /// <summary>
        /// Returns the value of the first column of the first row
        /// <para>返回首行首列的值</para>
        /// </summary>
        public T ExeScalar <T>()
        {
            CheckDisposed();
            AopResult aopResult = SetAopResult(AopEnum.ExeScalar);

            if (aopResult == AopResult.Default || aopResult == AopResult.Continue)
            {
                _aop.Para.ExeResult = dalHelper.ExeScalar(_procName, _isProc);
                _aop.Para.IsSuccess = _aop.Para.ExeResult != null;
            }
            if (aopResult == AopResult.Continue || aopResult == AopResult.Break)
            {
                _aop.End(AopEnum.ExeScalar);
            }
            if (_aop.Para.ExeResult == null || _aop.Para.ExeResult == DBNull.Value)
            {
                return(default(T));
            }
            Type   t     = typeof(T);
            object value = _aop.Para.ExeResult;

            switch (t.Name)
            {
            case "Int32":
                int intValue = 0;
                if (!int.TryParse(Convert.ToString(value), out intValue))
                {
                    return(default(T));
                }
                value = intValue;
                break;

            default:
                try
                {
                    value = ConvertTool.ChangeType(value, t);
                }
                catch
                {
                }

                break;
            }
            return((T)value);
        }
Beispiel #5
0
        /// <summary>
        /// 获得一个Cache对象
        /// </summary>
        public T Get <T>(string key)
        {
            object o = Get(key);

            if (o != null)
            {
                Type t = typeof(T);
                try
                {
                    return((T)ConvertTool.ChangeType(o, t));
                }
                catch (Exception err)
                {
                    Log.Write(err, LogType.Cache);
                    return(default(T));
                }
            }
            return(default(T));
        }
Beispiel #6
0
        public static T Query <T>(string key, T defaultValue, bool filter)
        {
            string value = HttpContext.Current.Request[key];

            if (value == null)
            {
                return(defaultValue);
            }
            value = value.Trim();
            object result = null;
            Type   t      = typeof(T);

            if (t.Name == "String")
            {
                if (filter)
                {
                    result = FilterValue(value);
                }
                else
                {
                    string reKey = "[#{@!}#]";
                    string text  = value.Replace("+", reKey);//
                    result = HttpContext.Current.Server.UrlDecode(text).Replace(reKey, "+");
                }
            }
            else
            {
                try
                {
                    result = ConvertTool.ChangeType(value, t);
                }
                catch
                {
                    return(defaultValue);
                }
            }
            return((T)result);
        }
Beispiel #7
0
        public static T Query <T>(string key, T defaultValue, string fromUrlQuery)
        {
            if (!string.IsNullOrEmpty(fromUrlQuery))
            {
                int startIndex = Math.Max(fromUrlQuery.IndexOf("&" + key + "=", StringComparison.OrdinalIgnoreCase), fromUrlQuery.IndexOf("?" + key + "=", StringComparison.OrdinalIgnoreCase));
                if (startIndex > -1)
                {
                    string result = string.Empty;
                    int    end    = fromUrlQuery.IndexOf("&", startIndex + key.Length + 2);
                    if (end == -1)
                    {
                        result = fromUrlQuery.Substring(startIndex + key.Length + 2);
                    }
                    else
                    {
                        result = fromUrlQuery.Substring(startIndex + key.Length + 2, end - (startIndex + key.Length + 2));
                    }
                    return(ConvertTool.ChangeType <T>(result));
                }
            }

            return(defaultValue);
        }
Beispiel #8
0
 internal static object ChangeType(object value, Type t)
 {
     return(ConvertTool.ChangeType(value, t));
 }
Beispiel #9
0
        /// <summary>
        ///  值的数据类型转换。
        /// </summary>
        /// <param name="value">要被转换的值</param>
        /// <param name="convertionType">要转换成哪种类型</param>
        /// <param name="groupID">数据库类型的组号</param>
        /// <returns></returns>
        internal object ChangeValue(object value, Type convertionType, int groupID, out Exception ex)
        {
            ex          = null;
            StringValue = Convert.ToString(value);
            if (value == null)
            {
                CellValue.IsNull = true;
                return(value);
            }
            if (groupID > 0 && StringValue == "")
            {
                CellValue.IsNull = true;
                return(null);
            }
            try
            {
                #region 类型转换
                if (groupID == 1)
                {
                    switch (StringValue)
                    {
                    case "正无穷大":
                        StringValue = "Infinity";
                        break;

                    case "负无穷大":
                        StringValue = "-Infinity";
                        break;
                    }
                }
                Type valueType = value.GetType();
                if (valueType.IsEnum && groupID == 1)
                {
                    if (convertionType.Name.StartsWith("Int"))
                    {
                        return((int)value);
                    }
                    return(Convert.ChangeType((int)value, convertionType));
                }
                if (valueType != convertionType)
                {
                    #region 折叠
                    switch (groupID)
                    {
                    case 0:
                        if (_CellStruct.SqlType == SqlDbType.Time)    //time类型的特殊处理。
                        {
                            string[] items = StringValue.Split(' ');
                            if (items.Length > 1)
                            {
                                StringValue = items[1];
                            }
                        }
                        value = StringValue;
                        break;

                    case 1:
                        switch (StringValue.ToLower())
                        {
                        case "true":
                            value = 1;
                            break;

                        case "false":
                            value = 0;
                            break;

                        case "infinity":
                            value = double.PositiveInfinity;
                            break;

                        case "-infinity":
                            value = double.NegativeInfinity;
                            break;

                        default:
                            goto err;
                        }
                        break;

                    case 2:
                        switch (StringValue.ToLower().TrimEnd(')', '('))
                        {
                        case "now":
                        case "getdate":
                        case "current_timestamp":
                            value = DateTime.Now;
                            break;

                        default:
                            DateTime dt = DateTime.Parse(StringValue);
                            value = dt == DateTime.MinValue ? (DateTime)SqlDateTime.MinValue : dt;
                            break;
                        }
                        break;

                    case 3:
                        switch (StringValue.ToLower())
                        {
                        case "yes":
                        case "true":
                        case "1":
                        case "on":
                        case "是":
                            value = true;
                            break;

                        case "no":
                        case "false":
                        case "0":
                        case "":
                        case "否":
                        default:
                            value = false;
                            break;
                        }
                        break;

                    case 4:
                        if (StringValue == SqlValue.Guid || StringValue.StartsWith("newid"))
                        {
                            value = Guid.NewGuid();
                        }
                        else
                        {
                            value = new Guid(StringValue);
                        }
                        break;

                    default:
err:
                        if (convertionType.Name.EndsWith("[]"))
                        {
                            value       = Convert.FromBase64String(StringValue);
                            StringValue = "System.Byte[]";
                        }
                        else
                        {
                            value = ConvertTool.ChangeType(value, convertionType);
                        }
                        break;
                    }
                    #endregion
                }
                //else if (groupID == 2 && strValue.StartsWith("000"))
                //{
                //    value = SqlDateTime.MinValue;
                //}
                #endregion
            }
            catch (Exception err)
            {
                value            = null;
                CellValue.Value  = null;
                CellValue.IsNull = true;
                ex = err;
                string msg = string.Format("ChangeType Error:ColumnName【{0}】({1}) , Value:【{2}】\r\n", _CellStruct.ColumnName, _CellStruct.ValueType.FullName, StringValue);
                StringValue = null;
                Log.Write(msg, LogType.Error);
            }
            return(value);
        }