AddColumn Method

Overview

The AddColumn method adds a new column to the table schema.

If the table already contains rows, the method back-fills the newly added column into each existing row so that the row value dictionaries remain aligned with the schema.

Declaration

/// <summary>
/// Adds column
/// </summary>
/// <param name="columnName">Column name</param>
/// <param name="dataType">Data type</param>
/// <param name="isNullable">Flag indicating whether column is nullable</param>
/// <param name="isPrimaryKey">Flag indicating whether column is primary key</param>
/// <param name="isForeignKey">Flag indicating whether column is foreign key</param>
/// <returns>Added column</returns>
public IColumnMetadata AddColumn(string columnName, string dataType, bool? isNullable = null, bool? isPrimaryKey = null, bool? isForeignKey = null)

Namespace and Assembly

Namespace: PocoDataSet.Data
Assembly: PocoDataSet.Data.dll

Behavior

Usage Example

// 1. Create an empty data set and a table
IDataSet dataSet = DataSetFactory.CreateDataSet();
IDataTable employeeTable = dataSet.AddNewTable("Employee");
employeeTable.AddColumn("Id", DataTypeNames.INT32);
employeeTable.AddColumn("FirstName", DataTypeNames.STRING);
employeeTable.AddColumn("LastName", DataTypeNames.STRING);

// 2. Add several rows to Employee data table
IDataRow employeeDataRow1 = employeeTable.AddNewRow();
employeeDataRow1["Id"] = 1;
employeeDataRow1["FirstName"] = "John";
employeeDataRow1["LastName"] = "Doe";

// Later, evolve schema by adding a new column after rows exist
employeeTable.AddColumn("NickName", DataTypeNames.STRING);

// The existing row has the new column key with null value
object? nick = employeeDataRow1["NickName"];

Notes

 

Table of Content POCO DataSet API References POCO DataSet Types DataTable Members

 


Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness or suitability of the information and are not responsible for any damages resulting from its use.