Oracle資料庫Clob欄位的更新操作

來源:互聯網
上載者:User

  OracleLob 與 OracleBFile 的區別在於前者的資料存放區在伺服器上而不是儲存在作業系統的物理檔案中。它也可以是一個讀寫對象,這一點與 OracleBFile 不同(後者始終為唯讀)。

  若要擷取 OracleLob 對象,請調用 GetOracleLob 方法。

  可以使用如下格式構造值為 NULL 的 OracleLob:
  OracleLob myLob = OracleLob.Null;
  測試從伺服器返回的 LOB 是否為 NULL
  if( myLob == OracleLob.Null)
  或者
  if( myLob.Value == DBNull.Value )
  NULL LOB 的行為與零位元組 LOB 的相似之處在於,Read 成功並始終返回零位元組。
  選擇一個包含空值的 LOB 列可返回 Null。
  必須在擷取臨時 LOB 之前開始事務。否則,OracleDataReader 將不能擷取後面的資料。

  1using System;
  2using System.Data;
  3using System.Data.OracleClient;
  4using System.IO;
  5
  6namespace ConnectOracle
  7{
  8    /// <summary>
  9    /// Oracle資料庫Clob欄位的更新操作
  10    /// </summary>
  11    public class ConnectionOracle
  12    {
  13
  14        public void UpdateClogData()
  15        {
  16
  17            //
  18            // 操作對象
  19            //
  20             OracleLob lob;
  21             OracleTransaction txn = null;
  22             OracleConnection conn = null;
  23             OracleCommand cmd = null;
  24             OracleDataReader dr = null;
  25            string strSql = string.Empty;
  26            string content = string.Empty;
  27            string CONNECTSTRING = "User ID=xxxx; Password=xxxx; Data Source=cmsdb_192.168.0.1";
  28
  29            try
  30            {
  31                 conn = new OracleConnection(CONNECTSTRING);
  32                 conn.Open();
  33                 txn = conn.BeginTransaction();
  34                 cmd = new OracleCommand(strSql,conn, txn);
  35
  36                //
  37                // 注意這裡的 FOR UPDATE 進行記錄鎖定
  38                //
  39                 cmd.CommandText = "SELECT content FROM mytable FOR UPDATE";
  40                 dr = cmd.ExecuteReader();
  41                 dr.Read();
  42
  43                while(dr.Read())
  44                {
  45                     lob = dr.GetOracleLob(0);
  46                    if(lob!=OracleLob.Null)
  47                    {
  48                         content = lob.Value.ToString();
  49
  50                        //
  51                        // 進行修改操作
  52                        //
  53                         content = "這是新的資料";
  54
  55                        //
  56                        // 將新的資料值轉換成byte[]
  57                        //
  58                        byte[] buffer = System.Text.Encoding.Unicode.GetBytes(content);
  59
  60                        //
  61                        // 寫回lob對象
  62                        //
  63                         lob.Write(buffer, 0, buffer.Length);
  64                     }
  65
  66                 }
  67                // 提交操作
  68                 txn.Commit();
  69                 Console.WriteLine("===============Success================");
  70             }
  71            catch(Exception ex)
  72            {
  73                 Console.WriteLine("Error: {0}", ex.ToString());
  74             }
  75            finally
  76            {
  77                 dr.Close();
  78                 conn.Close();
  79                 cmd.Dispose();
  80             }
  81         }
  82
  83     }
  84}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.