zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    elasticsearch安装教程
    mysql设置账号密码及授权
    mongodb设置账号密码授权案例
    新安装的centos 6.5,不能上网,外网ping不通,内网可以ping通解决方法
    docker-compose安装教程
    解决github图片不显示问题
    网站宽带计算方式
    thinkphp 如何实现url的rewrite
    nginx Https配置
    Ext.Net导入Excel
  • 原文地址:https://www.cnblogs.com/Godblessyou/p/1781046.html
Copyright © 2011-2022 走看看