public void AlterTableAddColumn(Type type, string propName, object defaultValue) { MdoReflector reflector = MdoReflector.get(type); MdoField f = reflector.Fields[propName]; string sql = string.Format("ALTER TABLE {0} ADD COLUMN {1} {2} DEFAULT {3}", reflector.TableName, propName.ToUpper(), SQLTypeName(f), GetSQLSafeValue(defaultValue)); ExeUpdateSQL(sql); }
public MdoReflector(Type _type) { Fields = new Dictionary <String, MdoField>(); GetterFields = new List <MdoField>(); SetterFields = new List <MdoField>(); DulpexFields = new List <MdoField>(); Type = _type; // 分析所有字段fields PropertyInfo[] pis = Type.GetProperties(); foreach (PropertyInfo pi in pis) { object[] piAttrs = pi.GetCustomAttributes(typeof(MdoFieldAttribute), false); if (piAttrs.Length == 0) { continue; } MdoField field = Fields[pi.Name] = new MdoField(pi); //可读属性集 if (pi.CanRead) { GetterFields.Add(field); } //可写属性集 if (pi.CanWrite) { SetterFields.Add(field); } //可读写属性集 if (pi.CanRead && pi.CanWrite) { DulpexFields.Add(field); } } // 分析数据表名 m_TableName = "tb_" + Type.Name.ToLower(); //tableName的缺省值 object[] attrs = Type.GetCustomAttributes(typeof(MdoAttribute), false); if (attrs.Length > 0) { MdoAttribute attr = (MdoAttribute)attrs[0]; m_TableName = attr.tableName; } }
private void readToField(DbDataReader reader, Object o, MdoField f) { int idx = reader.GetOrdinal(f.Name.ToUpper()); if (idx < 0) { throw new Exception("Can't find a field named '" + f.Name + "' in database table."); } object val = reader.GetValue(idx); if (val == null || val.GetType().Name == "DBNull") { f.setValue(o, null); return; } // 根据字段类型,对数值进行强制类型转换(Type Casting) switch (f.Type.Name) { case "String": val = reader.GetString(idx); break; case "SByte": val = (sbyte)reader.GetByte(idx); break; case "Byte": val = reader.GetByte(idx); break; case "Char": val = reader.GetChar(idx); break; case "Int16": val = reader.GetInt16(idx); break; case "UInt16": val = reader.GetInt16(idx); break; case "Int32": val = reader.GetInt32(idx); break; case "UInt32": val = reader.GetInt32(idx); break; case "Int64": val = reader.GetInt64(idx); break; case "UInt64": val = reader.GetInt64(idx); break; case "Single": val = reader.GetFloat(idx); break; case "Double": val = reader.GetDouble(idx); break; case "Boolean": val = reader.GetBoolean(idx); break; case "DateTime": val = reader.GetDateTime(idx); break; } if (f.Type.IsEnum) { try { val = Enum.Parse(f.Type, reader.GetString(idx)); } catch (InvalidCastException e) { GlobalLog.Debug(e.StackTrace); } } f.setValue(o, val); }
public static string SQLTypeName(MdoField f) { if (f.Type == typeof(sbyte)) { return("BIGINT"); } else if (f.Type == typeof(byte)) { return("BIGINT"); } else if (f.Type == typeof(char)) { return("BIGINT"); } else if (f.Type == typeof(short)) { return("BIGINT"); } else if (f.Type == typeof(ushort)) { return("BIGINT"); } else if (f.Type == typeof(int)) { return("BIGINT"); } else if (f.Type == typeof(uint)) { return("BIGINT"); } else if (f.Type == typeof(long)) { return("BIGINT"); } else if (f.Type == typeof(ulong)) { return("BIGINT"); } else if (f.Type == typeof(float)) { return("FLOAT"); } else if (f.Type == typeof(double)) { return("FLOAT"); } else if (f.Type == typeof(bool)) { return("BIGINT"); } else if (f.Type == typeof(DateTime)) { return("DATETIME "); } else if (f.Type == typeof(String)) { return("VARCHAR(" + f.Len + ") "); } else { return("VARCHAR(" + f.Len + ") "); } }
public NullKeyException(MdoField _field) { this._field = _field; }