Knight Data Access Layer
Adding Columns
Basic Tasks C# > Creating Database Structure > Adding Columns

Below is an example of semi automatically adding a column to a database table.  See the Synchronizing Tables section for how to automatically add missing columns to a table.  All ADO.NET DbTypes are automatically mapped to the specific database provider (SqliteProvider, SqlServerProvider etc.).  The same code can be used for different types of database providers.   

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

 Table table = db.mapper.CreateClassMap(typeof(Customer));

db.mapper.SetDefaultStringLength(table);

 

Field field = new Field();

field.ColumnName = "FavoriteColor";

field.DatabaseType = DbType.AnsiString;

field.Length = 20;

 

string sql = provider.GetAddColumnSql(table, field);

 

db.ExecuteNonQuery(sql);