我正在嘗試導出存儲在遠程SQL Server中的圖像的BLOB數據,並將它們保存到本地機器上的.jpg文件導出文件夾中。任何幫助排除第一個錯誤或幫助我修改工作連接以獲取blob數據並將其轉換爲本地計算機上的.jpgs將非常感激。SQL數據庫導出使用PowerShell
我發現下面的代碼
$Server = "?????"
$Database = "??????"
$Dest = "c:\Export\"
$bufferSize = 8192
$sqlCommand = " SELECT ID?????1
FROM dbo.W?????e; "
$authentication = "Integrated Security=SSPI;"
$connectionString = "Provider=sqloledb; " +
"Data Source=$dataSource; " +
"Initial Catalog=$database; " +
"$authentication; "
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection
$connection.Open()
$reader = $command.ExecuteReader()
$out = [array]::CreateInstance('Byte', $bufferSize)
While ($reader.Read())
{
$fileStream = New-Object System.IO.FileStream ($Dest + $reader.GetString(0)), Create, Write
$binaryWriter = New-Object System.IO.BinaryWriter $fileStream
$start = 0
$received = $reader.GetBytes(1, $start, $out, 0, $bufferSize - 1)
While ($received -gt 0)
{
$binaryWriter.Write($out, 0, $received)
$binaryWriter.Flush()
$start += $received
$received = $reader.GetBytes(1, $start, $out, 0, $bufferSize - 1)
}
$binaryWriter.Close()
$fileStream.Close()
}
$fileStream.Dispose()
$reader.Close()
$command.Dispose()
$connection.Close()
運行代碼時收到錯誤。
我可以使用此代碼連接到數據庫,但我不明白的streamprocess足以網格代碼做我想做的
$Server = "?????"; # SQL Server Instance.
$Database = "?????";
$Dest = "c:\Export\"; # Path to export to.
$bufferSize = 8192; # Stream buffer size in bytes.
$connString = "data source=?????,1433;Initial catalog=?????;Integrated Security=TRUE;"
$QueryText = "SELECT ID?????1
FROM dbo.W??????e;"
$SqlConnection = new-object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connString
$SqlCommand = $SqlConnection.CreateCommand()
$SqlCommand.CommandText = $QueryText
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCommand
$dataset = new-object System.Data.Dataset
$DataAdapter.Fill($dataset)
$dataset.Tables[0]
Write-Output ("Finished");
這就是我試圖完成,但我我得到的錯誤將字節轉換爲字符串 HERE
請將你的錯誤信息複製/粘貼到你的問題中。這樣,它就是Google的索引,並且有相同問題和錯誤消息的人可能會遇到它。 – 2014-09-05 21:08:56