Welcome 微信登录

首页 / 数据库 / MySQL / Oracle数据库Clob字段的更新操作

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}Oracle job 定时删除某条记录MySQL表联结自身相关资讯      oracle 
  • [INS-32052] Oracle基目录和Oracle  (07/22/2014 07:41:41)
  • Oracle 4个大对象(lobs)数据类型  (02/03/2013 12:33:05)
  • Oracle按时间段分组统计  (07/26/2012 10:36:48)
  • [Oracle] dbms_metadata.get_ddl的  (07/12/2013 07:37:30)
  • Liferay Portal 配置使用Oracle和  (07/31/2012 20:07:18)
  • Concurrent Request:Inactive   (07/20/2012 07:44:05)
本文评论 查看全部评论 (0)
表情: 姓名: 字数