Categories

Delete an Access Column

We have seen how to add a column to our access database – what about removing a column?

001
002
003
004
005
006
007
008
009
010
011
012
function Remove-AccessColumn {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
    [string]$table,
    [string]$column,
    [System.Data.OleDb.OleDbConnection]$connection
)
    $sql = "ALTER TABLE $table DROP COLUMN $column"
    $cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
   
    if ($psCmdlet.ShouldProcess("$($connection.DataSource)", "$sql")){$cmd.ExecuteNonQuery()}
}

 

We use the SupportsShouldProcess=$true to get the –whatif functionality.  Parameters are table, column name and the connection.

We create the SQL and either run it or perform the –whatif processing.  Remember we also get –confirm if we require it.

Leave a Reply