string GetColumnDefinition(Column column) { var sb = new StringBuilder(); sb.Append("\r\n\t"); sb.AppendFormat("[{0}] {1}", column.Name, GetDatabaseType(column)); if(!column.Nullable) sb.Append(" NOT"); sb.Append(" NULL"); if(column.PrimaryKey) sb.Append(" PRIMARY KEY"); if (column.Identity) sb.Append(" IDENTITY(1,1)"); if (column.Unique) sb.Append(" UNIQUE"); if (column.Default != null) sb.AppendFormat(" DEFAULT {0}", column.Default); return sb.ToString(); }
string GetDatabaseType(Column column) { switch (column.Type) { case DbType.Int16: return "tinyint"; case DbType.Int32: return "int"; case DbType.Int64: return "bigint"; case DbType.Boolean: return "bit"; case DbType.StringFixedLength: return string.Format("nchar({0})", column.Length); case DbType.String: if (column.Length < 4000) return string.Format("nvarchar({0})", column.Length); return "text"; case DbType.DateTime: return string.Format("datetime"); case DbType.Double: return string.Format("float"); case DbType.Decimal: return string.Format("decimal({0}, {1})", column.Precision, column.Scale); case DbType.Guid: return "uniqueidentifier"; case DbType.Currency: return "money"; case DbType.Xml: return "xml"; case DbType.Binary: return string.Format("varbinary({0})", column.Length <= 8000 ? column.Length.ToString() : "max"); default: throw new ArgumentOutOfRangeException("Don't know how to handle DbType: " + column.Type); } }