Oracle中Clob與Blob欄位的讀取

來源:互聯網
上載者:User

Oracle中若以字串的形式寫入資料到Clob欄位中,預設的長度為4000,最大可儲存4G。

     直接將字串寫入Clob欄位中,則最大可寫4000個位元組;若要寫入大的字元資料,可以將字元資料以二進位流的形式寫入。

寫入Clob(Blob代碼):

OracleConnection conn = new OracleConnection(GetConfigUserConnectionString());
            conn.Open();
            OracleTransaction tran = conn.BeginTransaction();
            string id = string.Empty;
            OracleCommand command = null;
            try
            {
                command = new OracleCommand();
                command.Connection = conn;
                double scale = 0;
                if (ct.Scale.IndexOf(":") == -1)
                {
                    scale = BasicOperate.GetDouble(ct.Scale, true);
                }
                else
                {
                    scale = BasicOperate.GetDouble(ct.Scale.Substring(ct.Scale.IndexOf(":") + 1), true);
                }

//在插入時先存一個空的empty_clob()[empty_blob()]
                command.CommandText = string.Format(@"insert into chart(chartname,charttype,source,scale,producttime,"
                    +"xml_definition) values('{0}','{1}','{2}',{3},'{4}',empty_clob()) "
                    +"returning chartid into :rid",
                     ct.ChartName, ct.ChartType, ct.Source, scale, ct.ProductTime, ct.xmlDefinition);
                command.Parameters.Add("rid", OracleDbType.Varchar2, 32).Direction = ParameterDirection.Output;
                command.ExecuteNonQuery();
                id = command.Parameters["rid"].Value.ToString();

//讀入clob欄位進行編輯
                command.CommandText = string.Format("select xml_definition from chart where chartid='{0}' for update", id);
                using (OracleDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        OracleClob clob = reader.GetOracleClob(0);//讀入二進位對性
                        clob.Erase();//清空其中的資料
                        clob.Position = 0;
                        clob.BeginChunkWrite();//開始寫入
                        int buffersize = 100;
                        int retval = 0;
                        byte[] bts = new byte[buffersize];

//將字串序列化為二進位流
                        MemoryStream stream = new MemoryStream();
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, ct.xmlDefinition);

//將二進位流寫入Clob字元中
                        stream.Seek(0, SeekOrigin.Begin);
                        retval = stream.Read(bts, 0, buffersize);
                        while (retval == buffersize)
                        {
                            clob.Write(bts, 0, buffersize);
                            retval = stream.Read(bts, 0, buffersize);
                        }
                            clob.Write(bts, 0, 100);
                        clob.EndChunkWrite();//結束寫入
                        clob.Flush();//重新整理
                        clob.Close();//關閉 
                    }
                    reader.Close();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                //throw new Exception(ex.Message);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }

 

 

讀取Clob(blob)欄位:

 

。。。 

 using (OracleDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        OracleClob clob = reader.GetOracleClob(0);//讀取二進位欄位
                        clob.Position = 0;//指向起點
                        byte[] tt = new byte[clob.Length];
                        clob.Read(tt, 0, (int)clob.Length);//將Clob讀為位元據
                        MemoryStream ms = new MemoryStream(tt);
                        BinaryFormatter bb = new BinaryFormatter();
                        object oo = bb.Deserialize(ms);//還原序列化取出字元資料

                    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.