public override int CreateTable(Type table, string tableName) { if (TableContians(table, tableName)) { return(0); } try { StringBuilder sb = new StringBuilder("create table ["); sb.Append(tableName); sb.Append("] ("); bool firstItem = true; table.GetProperties().ToList().ForEach(s => { if (!firstItem) { sb.Append(" , "); } firstItem = false; sb.Append(" ["); sb.Append(s.Name); sb.Append("] "); DBValueType valueType = Attribute.GetCustomAttribute(s, typeof(DBValueType)) as DBValueType; if (valueType != null) { sb.Append(valueType.ValueType); } else { string propType = s.PropertyType.Name.ToLower(); switch (propType) { case "int32": sb.Append("int"); break; case "float": case "float?": sb.Append("float"); break; case "string": sb.Append("nvarchar(1000)"); break; case "datetime": sb.Append("datetime"); break; case "byte[]": sb.Append("binary(8000)"); break; default: sb.Append(propType); break; } } PrimaryKey attrPrimaryKey = Attribute.GetCustomAttribute(s, typeof(PrimaryKey)) as PrimaryKey; if (attrPrimaryKey != null) { sb.Append(" primary key"); } Identity attrIdentity = Attribute.GetCustomAttribute(s, typeof(Identity)) as Identity; if (attrIdentity != null) { sb.Append(" identity("); sb.Append(attrIdentity.Seed); sb.Append(","); sb.Append(attrIdentity.Value); sb.Append(")"); } if (Attribute.GetCustomAttribute(s, typeof(Unique)) != null) { sb.Append(" unique"); } if (Attribute.GetCustomAttribute(s, typeof(ValueNotNull)) != null) { sb.Append(" not null"); } if (attrPrimaryKey == null) { ForeignKey attrForeignKey = Attribute.GetCustomAttribute(s, typeof(ForeignKey)) as ForeignKey; if (attrForeignKey != null) { sb.Append(" FOREIGN KEY REFERENCES "); sb.Append(attrForeignKey.TableName); sb.Append("("); sb.Append(attrForeignKey.Id); sb.Append(")"); } } Default attrDefault = Attribute.GetCustomAttribute(s, typeof(Default)) as Default; if (attrDefault != null) { sb.Append(" default("); sb.Append(attrDefault.Value); sb.Append(")"); } }); sb.Append(")"); return(dbExe.ExeNonQuery(null, sb.ToString(), null)); } catch (Exception) { throw; } }
public DBObject(string columnName, DBValueType valueType, string value) { this.ColumnName = columnName.ToLower(); this.ValueType = valueType; this.Value = value; }