Exemplo n.º 1
0
        private static void CheckFieldInfo(DataColumn dataColumn, string caption, I3DataTypeEnum dataType, int fieldLength)
        {
            switch (dataType)
            {
            case I3DataTypeEnum.sdtString:
                dataColumn.DataType     = typeof(string);
                dataColumn.DefaultValue = "";
                dataColumn.Caption      = caption;
                dataColumn.MaxLength    = fieldLength;
                break;

            case I3DataTypeEnum.sdtInt:
                dataColumn.DataType     = typeof(int);
                dataColumn.DefaultValue = 0;
                dataColumn.Caption      = caption;
                break;

            case I3DataTypeEnum.sdtDateTime:
                dataColumn.DataType     = typeof(DateTime);
                dataColumn.DefaultValue = new DateTime(2000, 1, 1);
                dataColumn.Caption      = caption;
                break;

            case I3DataTypeEnum.sdtFloat:
                dataColumn.DataType     = typeof(double);
                dataColumn.DefaultValue = 0;
                dataColumn.Caption      = caption;
                break;

            case I3DataTypeEnum.sdtBoolean:
                dataColumn.DataType     = typeof(bool);
                dataColumn.DefaultValue = false;
                dataColumn.Caption      = caption;
                break;

            case I3DataTypeEnum.sdtStream:
                dataColumn.DataType     = typeof(byte[]);
                dataColumn.DefaultValue = null;
                dataColumn.Caption      = caption;
                break;

            default:
                dataColumn.DataType     = typeof(string);
                dataColumn.DefaultValue = "";
                dataColumn.Caption      = caption;
                dataColumn.MaxLength    = 50;
                break;
            }
        }
Exemplo n.º 2
0
        ///检查DataColumn的类型的长度是否与目标类型、长度一致  一致时返回true
        private static bool CheckFieldInfoEqual(DataColumn column, I3DataTypeEnum dataType, int fieldLength)
        {
            switch (dataType)
            {
            case I3DataTypeEnum.sdtString:
                if ((column.DataType == typeof(string)) && (column.MaxLength == fieldLength))
                {
                    return(true);
                }
                break;

            case I3DataTypeEnum.sdtInt:
                if (column.DataType == typeof(int))
                {
                    return(true);
                }
                break;

            case I3DataTypeEnum.sdtDateTime:
                if (column.DataType == typeof(DateTime))
                {
                    return(true);
                }
                break;

            case I3DataTypeEnum.sdtFloat:
                if (column.DataType == typeof(double))
                {
                    return(true);
                }
                break;

            case I3DataTypeEnum.sdtBoolean:
                if (column.DataType == typeof(bool))
                {
                    return(true);
                }
                break;

            default:
                if ((column.DataType == typeof(string)) && (column.MaxLength == 50))
                {
                    return(true);
                }
                break;
            }

            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 检查字段是否存在
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fieldType"></param>
        /// <param name="fieldLength"></param>
        public static void CheckField(DataTable dataTable, string fieldName, string caption, I3DataTypeEnum dataType, int fieldLength)
        {
            bool       have;
            DataColumn testdataColumn = null;

            try
            {
                testdataColumn = dataTable.Columns[fieldName];
                have           = testdataColumn != null;
            }
            catch (Exception)
            {
                have = false;
            }


            if (!have)
            {
                DataColumn dataColumn = new DataColumn(fieldName);
                CheckFieldInfo(dataColumn, caption, dataType, fieldLength);
                dataTable.Columns.Add(dataColumn);
            }
            else
            {
                testdataColumn.Caption = caption;
                //主键不可修改
                foreach (DataColumn col in dataTable.PrimaryKey)
                {
                    if (col == testdataColumn)
                    {
                        return;
                    }
                }

                //数据类型和长度相同不用修改
                if (CheckFieldInfoEqual(testdataColumn, dataType, fieldLength))
                {
                    return;
                }

                DataColumn dataColumn = new DataColumn(fieldName + "2");
                CheckFieldInfo(dataColumn, caption, dataType, fieldLength);
                dataTable.Columns.Add(dataColumn);
                string errString = "";
                if (!CopyFieldValue(dataTable, testdataColumn, dataColumn, ref errString))
                {
                    dataTable.Columns.Remove(dataColumn);
                    if (errString.Length > 1000)
                    {
                        errString = I3StringUtil.SubString(errString, 0, 1000) + "\r\n.........";
                    }
                    errString = "修改字段" + fieldName + "时出现错误!错误信息:\r\n" + errString;
                    MessageBox.Show(errString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    dataTable.Columns.Remove(testdataColumn);
                    dataColumn.ColumnName = fieldName;
                }
            }
        }