我正在运行一个powershell程序,用于将数据从Excel插入数据库:
param([int]$StepNumber=1)
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$datestr=$(Get-Date).ToString("yyyy-MM-dd")
$url="http://www.dtcc.com/~/media/Files/Downloads/client-center/NSCC/NSCC-MPID-Directory.xls"
$destination = $directorypath +"/NSCC-MPID-Directory"+"_"+$datestr+".xls"
## if destination does not exsist, creat a new one.
if (!(Test-Path $destination) ) {
New-Item $destination -type file -force
}
$client = new-object System.Net.WebClient
$client.DownloadFile( $url, $destination)
$filepath = $destination
$excelConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$filepath;Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'"
$sqlConnection = 'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SIAC;Data Source=database1;'
$excelQuery = 'select * from [OTC$]'
$tablename = 'NSCC_MPID_OTC'
Try {
$conn = New-Object System.Data.OleDb.OleDbConnection($excelConnection)
$conn.open()
$cmd = $conn.CreateCommand()
$cmd.CommandText = $excelQuery
$rdr = $cmd.ExecuteReader()
$sqlbc = New-Object System.Data.SqlClient.SqlBulkCopy($sqlConnection, [System.Data.SqlClient.SqlBulkCopyOptions]::TableLock)
$sqlbc.DestinationTableName = $tableName
#$sqlbc.Batchsize = 1000
#$sqlbc.BulkCopyTimeout = 60
#$sqlbc.EnableStreaming=$true
# add all columns - you can add as few as you like.
for ($i = 0; $i -lt $rdr.FieldCount; $i++) {
$fname = $rdr.GetName($i)
Write-Host $fname -ForegroundColor green
[void]$sqlbc.ColumnMappings.Add($fname, $fname)
}
# write all of the data to the table
Try {
$sqlbc.WriteToServer($rdr)
} Catch {
Write-Host "$_" -Fore red -back white
} Finally {
$rdr.Close()
}
} Catch {
Write-Host "$_" -ForegroundColor red
}
当我运行需要使用OLE DB提供程序的程序时,它会给出错误:“Microsoft.ACE.OLEDB.12.0提供程序未在本地计算机上注册。有人可以告诉我为什么它找不到提供者已经存在。
我尝试下载Microsoft Database Access Engine 2017和Microsoft Database Access Engine 2010 Redistributible,但仍然有相同的错误。我也重新启动计算机并尝试以管理员身份运行....没有任何事情适合我。
有人可以帮我吗?感谢。