0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ODBCドライバーを使ったSQL実装

Posted at

前回も似たようなの作った気がする

odbc.ps1
# Load the required .NET assembly
Add-Type -Path "path/to/Oracle.ManagedDataAccess.dll"

# Connection string and query parameters
$connectionString = "User Id=your_user;Password=your_password;Data Source=your_data_source"
$query = "SELECT your_blob_column FROM your_table WHERE your_condition"
$outputFilePath = "path/to/output/file"

try {
    # Create and open the Oracle connection
    $conn = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)
    $conn.Open()

    # Create the Oracle command
    $cmd = $conn.CreateCommand()
    $cmd.CommandText = $query

    # Execute the query and get the data reader
    $reader = $cmd.ExecuteReader([System.Data.CommandBehavior]::SequentialAccess)

    if ($reader.Read()) {
        $bufferSize = 1024 * 1024 # 1MB buffer
        $buffer = New-Object byte[]($bufferSize)
        $fieldOffset = 0
        $bytesRead = 0

        # Create a file stream to write the BLOB data
        $fs = [System.IO.File]::Create($outputFilePath)

        do {
            $bytesRead = $reader.GetBytes(0, $fieldOffset, $buffer, 0, $bufferSize)
            $fs.Write($buffer, 0, $bytesRead)
            $fieldOffset += $bytesRead
        } while ($bytesRead -gt 0)

        # Close the file stream
        $fs.Close()

        Write-Output "BLOB data has been successfully written to the file."
    } else {
        Write-Output "No data found."
    }

    # Close the data reader and the connection
    $reader.Close()
    $conn.Close()
} catch {
    Write-Output "An error occurred: $_"
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?