Knight Data Access Layer
Adding Columns VB.NET
Basic Tasks VB.NET > Creating Database Structure > Adding Columns VB.NET

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

Dim db As IDataHelper = DataHelper.SessionFactory()

 

Dim table As Table = db.mapper.CreateClassMap(GetType(Customer))

db.mapper.SetDefaultStringLength(table)

 

Dim field As New Field()

field.ColumnName = "FavoriteColor"

field.DatabaseType = DbType.AnsiString

field.Length = 20

 

Dim sql As String = provider.GetAddColumnSql(table, field)

 

db.ExecuteNonQuery(sql)