public List<DatabaseColumn> RetrieveColumns(string databaseName, string tableName) { List<DatabaseColumn> columns = new List<DatabaseColumn>(); Database database = Server.Databases[databaseName]; Table table = database.Tables[tableName]; foreach (Column col in table.Columns) { DatabaseColumn column = new DatabaseColumn { Name = col.Name, SqlColumnType = col.DataType.SqlDataType.ToString().ToLower(), SqlColumnTypeAndSize = SqlTypeConversion.ConvertSqlTypeToSqlNativeType(col.DataType.SqlDataType.ToString(), col.DataType), ColumnType = SqlTypeConversion.ConvertSqlTypesToCSharpShorthand(col.DataType.SqlDataType.ToString()), IsPrimaryKey = col.InPrimaryKey, IsForeignKey = col.IsForeignKey, IsNullable = col.Nullable, SqlClientDbType = SqlTypeConversion.ConvertSqlTypesToSqlClientDbType(col.DataType.SqlDataType.ToString()), Size = col.DataType.MaximumLength }; columns.Add(column); } return columns; }
private static string GenerateColumsWithoutPrimaryKey(DatabaseColumn databaseColumn, bool isLastColumn) { string column = string.Empty; //Ignore primary keys. I'm assuming they will be auto generated if (!databaseColumn.IsPrimaryKey) { column = GenrateParameterColumn(databaseColumn, isLastColumn); } return column; }
private static string GenrateParameterColumn(DatabaseColumn databaseColumn, bool isLastColumn) { //Last item in the collection string column = (isLastColumn ? string.Format("\t@{0} {1}", databaseColumn.Name, databaseColumn.SqlColumnTypeAndSize) : string.Format("\t@{0} {1},", databaseColumn.Name, databaseColumn.SqlColumnTypeAndSize)); return column; }