Example #1
0
File: Comm.cs Project: siszoey/GT
        /// <summary>
        /// 将DataRow中的数据转换为Struct
        /// </summary>
        /// <typeparam name="StructT"></typeparam>
        /// <param name="record"></param>
        /// <param name="row"></param>
        public static void ConvertDataRowToStruct <StructT>(ref StructT record, DataRow row)
        {
            Type recordType = record.GetType();

            PropertyInfo[] fieldInfos = recordType.GetProperties();
            object         recordOBJ  = (object)record;

            foreach (PropertyInfo fieldInfo in fieldInfos)
            {
                object[] attributes = fieldInfo.GetCustomAttributes(true);
                foreach (Attribute tempAttr in attributes)
                {
                    ColumnAttrib columnAttrib = tempAttr as ColumnAttrib;
                    if (columnAttrib != null && row.Table.Columns[columnAttrib.ColumnName] != null)
                    {
                        if (columnAttrib.IsViewColumn)
                        {
                            try
                            {
                                object tempObj = row[columnAttrib.ColumnName];
                            }
                            catch
                            {
                                break;
                            }
                        }

                        object fieldValue = null;
                        switch (fieldInfo.PropertyType.ToString())
                        {
                        case "System.Boolean":
                        case "System.Nullable`1[System.Boolean]":
                        case "System.Data.SqlTypes.SqlBoolean":
                            fieldValue = DBConvert.ToBoolean(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        case "System.Int16":
                        case "System.Nullable`1[System.Int16]":
                        case "System.Data.SqlTypes.SqlInt16":
                            fieldValue = DBConvert.ToInt16(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        case "System.Int32":
                        case "System.Nullable`1[System.Int32]":
                        case "System.Data.SqlTypes.SqlInt32":
                            fieldValue = DBConvert.ToInt32(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        case "System.String":
                        case "System.Nullable`1[System.String]":
                        case "System.Data.SqlTypes.SqlString":
                            fieldValue = DBConvert.ToString(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        case "System.DateTime":
                        case "System.Nullable`1[System.DateTime]":
                        case "System.Data.SqlTypes.SqlDateTime":
                            fieldValue = DBConvert.ToDateTime(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        //float
                        case "System.Single":
                        case "System.Nullable`1[System.Single]":
                        case "System.Data.SqlTypes.SqlSingle":
                            fieldValue = DBConvert.ToSingle(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        //Decimal
                        case "System.Decimal":
                        case "System.Nullable`1[System.Decimal]":
                        case "System.Data.SqlTypes.SqlDecimal":
                            fieldValue = DBConvert.ToDecimal(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        //byte[]
                        case "System.Byte[]":
                        case "System.Nullable`1[System.Byte[]]":
                        case "System.Data.SqlTypes.SqlBytes":
                            fieldValue = DBConvert.ToBytes(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;

                        //double
                        case "System.Double":
                        case "System.Nullable`1[System.Double]":
                        case "System.Data.SqlTypes.SqlDouble":
                            fieldValue = DBConvert.ToDouble(row[columnAttrib.ColumnName]);
                            fieldInfo.SetValue(recordOBJ, fieldValue);
                            break;
                        }
                    }
                }
            }
            record = (StructT)recordOBJ;
        }
Example #2
0
File: Comm.cs Project: siszoey/GT
        public static void ConvertStructToDataRow(object Record, DataRow Row)
        {
            Type recordType = Record.GetType();

            PropertyInfo[] fieldInfos = recordType.GetProperties();

            foreach (PropertyInfo fieldInfo in fieldInfos)
            {
                object[] attributes = fieldInfo.GetCustomAttributes(true);
                foreach (Attribute tempAttr in attributes)
                {
                    ColumnAttrib columnAttrib = tempAttr as ColumnAttrib;
                    if (columnAttrib != null && Row.Table.Columns[columnAttrib.ColumnName] != null)
                    {
                        if (!columnAttrib.IsViewColumn && columnAttrib.ColumnName.ToUpper() != "ID")
                        {
                            object vObjectValue = fieldInfo.GetValue(Record);
                            switch (fieldInfo.PropertyType.ToString())
                            {
                            case "System.Data.SqlTypes.SqlBoolean":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlBoolean)vObjectValue).Value;
                                break;

                            case "System.Data.SqlTypes.SqlInt16":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlInt16)vObjectValue).Value;
                                break;

                            case "System.Data.SqlTypes.SqlInt32":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlInt32)vObjectValue).Value;
                                break;

                            case "System.Data.SqlTypes.SqlString":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlString)vObjectValue).Value;
                                break;

                            case "System.Data.SqlTypes.SqlDateTime":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlDateTime)vObjectValue).Value;
                                break;

                            //float
                            case "System.Data.SqlTypes.SqlSingle":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlSingle)vObjectValue).Value;
                                break;

                            //Decimal
                            case "System.Data.SqlTypes.SqlDecimal":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlDecimal)vObjectValue).Value;
                                break;

                            //byte[]
                            case "System.Data.SqlTypes.SqlBytes":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlBytes)vObjectValue).Value;
                                break;

                            //double
                            case "System.Data.SqlTypes.SqlDouble":
                                Row[columnAttrib.ColumnName] = ((INullable)vObjectValue).IsNull ? (object)DBNull.Value : ((SqlDouble)vObjectValue).Value;
                                break;
                            }
                        }
                    }
                }
            }
        }