Now we have created our Table we can start adding columns
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 |
function New-AccessColumn {
[CmdletBinding()] param ( [System.Data.OleDb.OleDbConnection]$connection, [string]$table, [switch]$notnull, [parameter(ParameterSetName="datetime")] [parameter(ParameterSetName="unique")] [parameter(ParameterSetName="binary")] [parameter(ParameterSetName="bit")] [parameter(ParameterSetName="tinyinteger")] [parameter(ParameterSetName="smallinteger")] [parameter(ParameterSetName="double")] [parameter(ParameterSetName="real")] [parameter(ParameterSetName="float")] [parameter(ParameterSetName="image")] binary {$sql = "ALTER TABLE $table ADD COLUMN $binname BINARY" } tinyinteger {$sql = "ALTER TABLE $table ADD COLUMN $tnyintname TINYINT" } double {$sql = "ALTER TABLE $table ADD COLUMN $dblname DOUBLE" } |
The first set of parameters define the connection and table name. The $notnull parameter adds the NOT NULL statement to the SQL we create. This forces us to add a value for the column when we insert a row. These parameters are not part of a parameter set – this means they work with all parameter sets.
Parameter sets are used in this function to make the data types mutually exclusive – i.e. one call to Add-AccessColumn can add one column of one, and only one, data type. The size parameter applies to character and text data types so is a member of both parameter sets.
The parameter set is used in a switch statement to create the SQL to add a column of the correct type. If the $notnull switch is set we add the appropriate commands to the SQL.
With advanced functions in PowerShell v2 we get the ability to use the common parameters so I can use
New-AccessColumn -connection $db -table $table -dtname Mydate2 –notnull -Debug
and the Write-Debug statements will be triggered. In this case I get a dump of the SQL I have created. The function finishes by executing the SQL against the database as we have seen before.
As an example of using the function
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 |
Import-Module accessfunctions -Force
$db = Open-AccessDatabase -name test03.mdb -path c:\test $table = "NewTable2" New-AccessColumn -connection $db -table $table -dtname Mydate New-AccessColumn -connection $db -table $table -uniquename MyUnique New-AccessColumn -connection $db -table $table -binname MyBinary New-AccessColumn -connection $db -table $table -tnyintname MyTiny New-AccessColumn -connection $db -table $table -tnyintname MyTiny2 -notnull New-AccessColumn -connection $db -table $table -dblname MyDouble New-AccessColumn -connection $db -table $table -dblname MyDouble2 -notnull New-AccessColumn -connection $db -table $table -charname MyChar New-AccessColumn -connection $db -table $table -textname MyText New-AccessColumn -connection $db -table $table -imgname MyImg Close-AccessDatabase $db |
Producing this becomes easy when using cut and paste within an editor.
Next we will look at removing columns and tables