• c#操作oracle lob字段【转】


    public static void WriteClob(string table_name, string fieldname ,string table_id, string id, int column, string content)
    {
    OracleConnection myCn = OracleModel.GetConnection(DbKey.ORA);
    OracleCommand myCmd = new OracleCommand();
    try
    {
    myCn.Open();
    }
    catch (System.Data.OracleClient.OracleException e)
    {
    throw new Exception(e.Message);
    }
    OracleTransaction myTrans = myCn.BeginTransaction();
    try
    {
    myCmd.Connection = myCn;
    myCmd.Transaction = myTrans;
    myCmd.CommandText = "SELECT "+fieldname+" FROM " + table_name + " where " + table_id + " = '" + id + "' FOR UPDATE";
    OracleDataReader reader = myCmd.ExecuteReader();
    using (reader)
    {
    reader.Read();
    OracleLob CLOB = reader.GetOracleLob(column); //column为lob类型所在列
    Encoding utf16 = Encoding.Unicode;       
    byte[] buffer = utf16.GetBytes(content);
    if (buffer.Length % 2 == 0)
    CLOB.Write(buffer, 0, buffer.Length);  //这里的参数必须为偶数
    else
    CLOB.Write(buffer, 0, (buffer.Length - 1));
    myTrans.Commit();
    }
    }
    catch (System.Data.OracleClient.OracleException e)
    {
    myTrans.Rollback();
    }
    finally
    {
    myCmd.Dispose();
    myCn.Close();
    }
    }
    public static string ReadClob(string table_name, string fieldname,string table_id, string id, int column)
    {
    OracleConnection myCn = OracleModel.GetConnection(DbKey.ORA);
    OracleCommand myCmd = new OracleCommand();
    try
    {
    myCn.Open();
    }
    catch (System.Data.OracleClient.OracleException e)
    {
    throw new Exception(e.Message);
    }
    OracleTransaction myTrans = myCn.BeginTransaction();
    try
    {
    myCmd.Connection = myCn;
    myCmd.Transaction = myTrans;
    myCmd.CommandText = "SELECT "+fieldname+" FROM " + table_name + " where " + table_id + " = '" + id + "'";
    OracleDataReader reader = myCmd.ExecuteReader();
    using (reader)
    {
    reader.Read();
    OracleLob CLOB = reader.GetOracleLob(column);

    byte[] buffer = new byte[(int)CLOB.Length];
    CLOB.Read(buffer,0,(int)CLOB.Length);

    Encoding utf16 = Encoding.Unicode;
    string final = utf16.GetString(buffer);
    myTrans.Commit();
    return final;
    }
    }
    catch (System.Data.OracleClient.OracleException e)
    {
    myTrans.Rollback();
    return "";
    }
    finally
    {
    myCmd.Dispose();
    myCn.Close();
    }
    }

    原文来自:http://hi.baidu.com/qianko/blog/item/041e43ce55a60f0993457e2d.html

  • 相关阅读:
    Spring注解驱动开发3:自动装配
    Spring注解驱动开发2:生命周期和属性赋值
    Spring注解驱动开发1:组件注册
    Java线程及其实现方式
    Winform 可取消的单选按钮(RadioButton)
    autoit脚本-从基本的函数用法开始
    python进阶(一)
    dict字典的一些优势和劣势
    读《流畅的python》第一天
    智能化脚本autoit v3的简单了解
  • 原文地址:https://www.cnblogs.com/Godblessyou/p/1781046.html
Copyright © 2020-2023  润新知