GetOracleBlobForUpdate(int)
このメソッドは、指定されたBLOB
列の更新可能なOracleBlob
オブジェクトを戻します。
宣言
// C#
public OracleBlob GetOracleBlobForUpdate(int index);
パラメータ
-
index
ゼロベースの列索引です。
戻り値
更新可能なOracleBlob
オブジェクト
例外
InvalidOperationException
- 接続またはリーダーがクローズされているか、Read()
が呼び出されていないか、すべての行が読み取られています。
IndexOutOfRangeException
- 列索引が無効です。
InvalidCastException
- この列型のアクセッサ・メソッドが無効か、列値がNULL
です。
備考
OracleCommand
のExecuteReader()
メソッドを起動する場合、OracleDataReader
によってフェッチされる全データは、特定のスナップショットから導出されます。このため、アクセッサ・メソッドを同じ列で呼び出すと、常に同じ値が戻されます。GetOracleBlobForUpdate()
メソッドではデータベース・ラウンドトリップを発生させ、現行のBLOB
データに対する参照を取得する一方、FOR
UPDATE
句を使用して行のロックも行います。つまり、元のスナップショットからは値を取得できないため、GetOracleBlob()
から取得したOracleBlob
には、GetOracleBlobForUpdate()
から取得したOracleBlob
とは異なる値を含めることができます。
このメソッドをコール後にBLOB
列はロックされるため、戻されたOracleBlob
オブジェクトを使用して、BLOB
を安全に更新できます。
このメソッドを内部で起動すると、WAIT
句を使用せずにSELECT..FOR UPDATE
文が実行されます。このため、その行に対してロックが取得されるまで、文は無期限に待機できます。
このメソッドを呼び出す前にIsDBNull
を呼び出して、NULL
値を確認してください。
例
次の例では、更新のためリーダーからOracleBlob
オブジェクトを取得し、OracleBlob
オブジェクトを更新して、トランザクションをコミットしています。
/* Database Setup, if you have not done so yet. connect scott/tiger@oracle CREATE TABLE empInfo ( empno NUMBER(4) PRIMARY KEY, empName VARCHAR2(20) NOT NULL, hiredate DATE, salary NUMBER(7,2), jobDescription Clob, byteCodes BLOB ); Insert into empInfo(EMPNO,EMPNAME,JOBDESCRIPTION,byteCodes) values (1,'KING','SOFTWARE ENGR', '5657'); Insert into empInfo(EMPNO,EMPNAME,JOBDESCRIPTION,byteCodes) values (2,'SCOTT','MANAGER', '5960'); commit; */ // C# using System; using System.Data; using Oracle.DataAccess.Client; using Oracle.DataAccess.Types; class GetOracleBlobForUpdateSample { static void Main() { string constr = "User Id=scott;Password=tiger;Data Source=oracle"; OracleConnection con = new OracleConnection(constr); con.Open(); // Get the ByteCodes for empno = 1 string cmdstr = "SELECT BYTECODES, EMPNO FROM EMPINFO where EMPNO = 1"; OracleCommand cmd = new OracleCommand(cmdstr, con); // Since we are going to update the OracleBlob object, we will //have to create a transaction OracleTransaction txn = con.BeginTransaction(); // Get the reader OracleDataReader reader = cmd.ExecuteReader(); // Declare the variables to retrieve the data in EmpInfo OracleBlob byteCodesBlob; // Read the first row reader.Read(); if (!reader.IsDBNull(0)) { byteCodesBlob = reader.GetOracleBlobForUpdate(0); // Close the reader reader.Close(); // Update the ByteCodes object byte[] addedBytes = new byte[2] {0, 0}; byteCodesBlob.Append(addedBytes, 0, addedBytes.Length); // Now commit the transaction txn.Commit(); Console.WriteLine("Blob Column successfully updated"); } else reader.Dispose(); // Close the connection con.Dispose(); } }