檔案插入 Oracle資料庫 Blob型別

ForTechnology發表於2011-12-01
  string connStr = m_db.ConnectionString;
 OracleConnection raConn = new OracleConnection(connStr);
if (oraConn.State != ConnectionState.Open)
 {
                oraConn.Open();
            }
            OracleCommand cmd = oraConn.CreateCommand();
            OracleTransaction tx = oraConn.BeginTransaction();
            cmd.Transaction = tx;
            bool returnValue = false;
            try
            {
                cmd.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
                cmd.Parameters.Add(new OracleParameter("tempblob", OracleType.Blob)).Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                OracleLob tempLob = (OracleLob)cmd.Parameters[0].Value;
                tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
                tempLob.Write(p_content, 0, p_content.Length);
                tempLob.EndBatch();

                cmd.Parameters.Clear();
                cmd.CommandText = "...";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add.....
                cmd.ExecuteNonQuery();
  tx.Commit();
     returnValue=true;
            }
            catch (Exception ex)
            {
                returnValue = false;
                tx.Rollback();
                throw ex;
            }
            finally
            {
                oraConn.Close();
            }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25897606/viewspace-712518/,如需轉載,請註明出處,否則將追究法律責任。

相關文章