Create Blank Access Database Using Powershell

自闭症网瘾萝莉.ら 提交于 2019-12-25 02:43:40

问题


Normally I'd research the topic I need for a day or 2 and then ask here, but I've been struggling to find anything at all about this topic.

Does anyone know how to use PowerShell (V3) to create a new Access 2010 MDB file, with a defined table and column headings?

Thanks for any guidance.


回答1:


Found some! Just for interests sake for anyone checking, here is the code:

Function Create-DataBase($Db){
 $application = New-Object -ComObject Access.Application
 $application.NewCurrentDataBase($Db,10)
 $application.CloseCurrentDataBase()
 $application.Quit()
}
Function Invoke-ADOCommand($Db, $Command){
 $connection = New-Object -ComObject ADODB.Connection
 $connection.Open("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=$Db")
 $connection.Execute($command)
 $connection.Close()
}
$Db = "C:\ScheduledCodeRun\CollatedData\Test.mdb"
$table = "MyTest"
$Fields = "F1 Counter, F2 Date, F3 Integer, F4 Text"
$command = "Create Table $table($fields)"
If(Test-Path $Db){
    Write-Host 'DB already exists' -fore green
}else{
    Create-DataBase $db
    Invoke-ADOCommand $Db 'Create Table MyTest(F1 Counter, F2 Date, F3 Integer, F4 Text)'
}


来源:https://stackoverflow.com/questions/25277690/create-blank-access-database-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!