2

Powershell関数からスクリプトを呼び出すとC、データベース接続が開きません。ただし、プログラムを実行するユーザーとしてサーバーにログオンしC、DOS プロンプトから起動すると、すべてが期待どおりに機能します。

Cプログラムが を介してデータベース接続を開くことができないのはなぜですかC

サーバーは Windows 2008 R2 64 ビットです。

これが実行中のPowershellスクリプトです。どの部分が機能しているかを検証するためのデバッグ ログが作成されます。

$DEBUGStream = [System.IO.StreamWriter] "D:\powershell\DEBUG.txt"
$DEBUGStream.WriteLine("START DEBUG")

$ExcelFile = "D:\InterfaceData\excel\" + $args[0]
$Sheetname = "Sheet2$"
$OleDbConn = New-Object “System.Data.OleDb.OleDbConnection”
$OleDbCmd = New-Object “System.Data.OleDb.OleDbCommand”
$OleDbAdapter = New-Object “System.Data.OleDb.OleDbDataAdapter”
$DataTable = New-Object “System.Data.DataTable”

$OleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$ExcelFile`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
$OleDbConn.Open()

$DEBUGStream.WriteLine("Connection: $OleDbConn.State")
    $DEBUGStream.WriteLine("Environment: $env:Processor_Architecture")

$OleDbCmd.Connection = $OleDbConn
$OleDbCmd.commandtext = “Select * from [$Sheetname]”
$OleDbAdapter.SelectCommand = $OleDbCmd

$RowsReturned = $OleDbAdapter.Fill($DataTable)
$DEBUGStream.WriteLine("Data Table fill: Rows: $RowsReturned")
$intRow = 1
ForEach ($DataRec in $DataTable) {
    $DEBUGStream.WriteLine("DATA TABLE ROW $intRow")
    $intRow++
}

$DEBUGStream.WriteLine("Loop finished")

$OleDbConn.Close()

$DEBUGStream.WriteLine("All Closed")
$DEBUGStream.close()

DOSプロンプトから手動で実行したときの出力は次のとおりです

C:\Users\me>powershell -executionpolicy bypass d:\powershell\Interface.ps1 test.xlsx
START DEBUG
Connection: Open
    Environment: AMD64
Data Table fill: Rows: 4
DATA TABLE ROW 1
DATA TABLE ROW 2
DATA TABLE ROW 3
DATA TABLE ROW 4
Loop finished
All Closed

プログラムから呼び出されたときの出力を次に示しますC。コマンドを使用して呼び出しますsystem

START DEBUG
Connection: Closed
    Environment: x86
Data Table fill: Rows: 
Loop finished
All Closed

編集

64 ビットPowershellバージョンを実行し、コマンド プロンプトからスクリプトを実行すると、新しいエラーが発生します。Microsoft Access データベース エンジン 2010 (14.0.4763.1000) の 64 ビット バージョンをインストールしました。

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass d:\wcc_powershell\WRIRMPTruckInterface.ps1 test.xlsx

C:\Users\me>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass d:\powershell\Interface.ps1 
Exception calling "Open" with "0" argument(s): "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."
At D:\powershell\Interface.ps1:19 char:16
+ $OleDbConn.Open <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
4

1 に答える 1

2

「C」プログラムから実行すると、32 ビット プロセス (x86) として起動することがはっきりとわかります。32 ビットの OleDb ドライバーがインストールされていないため、これが当面の問題です。「C」プログラムを 64 ビット PE としてコンパイルする必要があります。これにより、powershell が 64 ビット プロセスとして起動されます。または、次のパスを使用して明示的に powershell を実行します。

c:\windows\sysnative\windowspowershell\v1.0\powershell.exe

これにより、32 ビットの「C」プログラムが強制的に powershell を 64 ビットの子プロセスとして起動します。

私の関連するブログ投稿は次のとおりです。シェル

于 2013-05-23T01:15:36.497 に答える