/// <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 = StaticTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType);
                        }
                        catch
                        {
                        }
                    }
                }
                result = true;

            }
            return result;
        }
Beispiel #2
0
        /// <summary>
        /// 验证基础数据(数据类型、长度、是否为Null)
        /// </summary>
        /// <returns></returns>
        public static bool ValidateData(MDataTable dt, MDataRow info)
        {
            bool result = true;

            string[]      tables       = null;
            List <string> requiredList = new List <string>();//必填项表。

            if (info != null)
            {
                tables = info.Get <string>(Config_Excel.TableNames, string.Empty).Split(',');
                MDataTable dtRequired = GetExcelInfo(info.Get <string>(0));//必填项表。
                if (dtRequired != null && dtRequired.Rows.Count > 0)
                {
                    dtRequired = dtRequired.Select("IsRequired=1");
                    if (dtRequired != null && dtRequired.Rows.Count > 0)
                    {
                        foreach (var row in dtRequired.Rows)
                        {
                            requiredList.Add(row.Get <string>("TableName") + row.Get <string>("Field"));
                        }
                    }
                }
            }
            else
            {
                tables = dt.TableName.Split(',');
            }
            bool isOK = false;

            foreach (var table in tables)//重置列头。
            {
                MDataColumn mdc = CYQ.Data.Tool.DBTool.GetColumns(table);
                foreach (var cs in dt.Columns)
                {
                    string[] items = cs.ColumnName.Split('.');
                    if (cs.TableName == table)
                    {
                        int index = mdc.GetIndex(items[items.Length - 1]);
                        if (index > -1)
                        {
                            isOK         = true;//至少需要一个列对应上,若没有,则模板错误
                            cs.SqlType   = mdc[index].SqlType;
                            cs.IsCanNull = mdc[index].IsCanNull;
                            if (requiredList.Contains(table + mdc[index].ColumnName))//要求必填
                            {
                                cs.IsCanNull = false;
                            }
                            cs.MaxSize = mdc[index].MaxSize;
                        }
                    }
                }
            }
            if (!isOK)
            {
                return(false);
            }
            foreach (var row in dt.Rows)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var cell in row)
                {
                    if (!string.IsNullOrEmpty(cell.Struct.TableName))
                    {
                        string columnName = string.IsNullOrEmpty(cell.Struct.Description) ? cell.Struct.ColumnName : cell.Struct.Description;
                        if (!cell.Struct.IsCanNull && cell.IsNullOrEmpty)
                        {
                            sb.AppendFormat("[{0}]不允许为空。", columnName);
                            cell.State = -1;
                        }
                        else if (cell.Struct.MaxSize != -1 && cell.ToString().Length > cell.Struct.MaxSize && cell.Struct.SqlType != System.Data.SqlDbType.Bit)
                        {
                            sb.AppendFormat("[{0}]长度超过{1}。", columnName, cell.Struct.MaxSize);
                            cell.State = -1;
                        }
                        else if (!cell.FixValue())
                        {
                            sb.AppendFormat("[{0}]数据类型错误。", columnName);
                            cell.State = -1;
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    result = false;
                    row.Set("错误信息", row.Get <string>("错误信息") + sb.ToString());
                }
            }
            return(result);
        }