博客园  :: 首页  :: 联系 :: 管理

运用C#处理lob数据类型 (Oracle)

Posted on 2006-04-26 14:40  sunrack  阅读(691)  评论(0编辑  收藏  举报

#region 更新blob字段 UpdateBlob(string strTableName, string blobColumn, byte[] blobValue, string strCondition)

  /// <summary>
  /// 更新blob字段
  /// </summary>
  /// <param name="strTableName">表名</param>
  /// <param name="blobColumn">列名</param>
  /// <param name="blobValue">Blob值</param>
  /// <param name="strCondition">条件</param>
  public static void UpdateBlob(string strTableName, string blobColumn, byte[] blobValue, string strCondition)
  {
   string strConnectionString ="user id= " + ProfileModel.strUserName + ";" +
    "data source=" + ProfileModel.strServerSource + " ;password= " +ProfileModel.strPassword ;

   string strSelectSql = "SELECT * FROM " + strTableName;
   strSelectSql += " WHERE " + strCondition;

   string strUpdateSql = "UPDATE " + strTableName + " SET " + blobColumn + " = :BlobValue ";  
   strUpdateSql += " WHERE " + strCondition;

   OracleConnection blobConnection = new OracleConnection(strConnectionString);;
   OracleDataAdapter blobAdapter;
   DataSet blobDataSet;
   DataTable blobTable;
   DataRow blobRow;

   try
   {
    blobConnection.Open();

    blobAdapter = new OracleDataAdapter(strSelectSql, blobConnection);
    
    blobAdapter.UpdateCommand = new OracleCommand(strUpdateSql, blobConnection);
    blobAdapter.UpdateCommand.Parameters.Add(":BlobValue", OracleType.Blob, blobValue.Length, blobColumn);

    blobDataSet = new DataSet(strTableName);

    blobAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
   
    blobAdapter.FillSchema(blobDataSet, SchemaType.Source, strTableName);

    blobAdapter.Fill(blobDataSet,strTableName);

    blobTable = blobDataSet.Tables[strTableName];
    blobRow = blobTable.Rows[0];
   
    blobRow.BeginEdit();
   
    if (blobValue.Length != 0)
    {
     blobRow[blobColumn] = blobValue;
    }
   
    blobRow.EndEdit();

    blobAdapter.Update(blobDataSet,strTableName);
   }
   catch(Exception e)
   {
    Console.WriteLine("DbAccess.UpdateBlob() Error" + e.Message);
    throw new Exception("DbAccess.UpdateBlob() Error" + e.Message);
   }
   finally
   {
    blobConnection.Close();
   }
  }
  #endregion

 #region 读取blob字段 byte[] ReadBlob(string strTableName, string blobColumn, string strCondition)

  /// <summary>
  ///  读取blob字段
  /// </summary>
  /// <param name="strTableName">表名</param>
  /// <param name="blobColumn">列名</param>
  /// <param name="strCondition">条件</param>
  public static byte[] ReadBlob(string strTableName, string blobColumn, string strCondition)
  {
   string strConnectionString ="user id= " + ProfileModel.strUserName + ";" +
    "data source=" + ProfileModel.strServerSource + " ;password= " +ProfileModel.strPassword ;

   string strSelectSql = "SELECT " + blobColumn + " FROM " + strTableName;
   strSelectSql += " WHERE " + strCondition + " AND DBMS_LOB.getlength( " + blobColumn + " ) > 0 ";

   OracleConnection blobConnection = new OracleConnection(strConnectionString);;

   try
   {
    blobConnection.Open();

    OracleCommand blobCmd = new OracleCommand();
    blobCmd.Connection = blobConnection;
    blobCmd.CommandText = strSelectSql;

    OracleDataReader blobReader = blobCmd.ExecuteReader();

    blobReader.Read();

    if(blobReader.HasRows == false)
    {
     return null;
    }
    byte[] blobValue = (byte[])blobReader[blobColumn];

    return blobValue;
   }
   catch(Exception e)
   {
    Console.WriteLine("DbAccess.UpdateBlob() Error" + e.Message);
    throw new Exception("DbAccess.UpdateBlob() Error" + e.Message);
   }
   finally
   {
    blobConnection.Close();
   }
  }
  #endregion

 #region 获取最新的答题记录

  public static bool GetLatestAnswerInfo(ref AnswerData CurAnswerData)
  {
   DataTable CurAnswerTable = AnswerSys.GetQuestionAnswerTable(CurAnswerData);

   if(CurAnswerTable == null || CurAnswerTable.Rows.Count == 0)
   {
    return false;
   }

   DataRow CurAnswer = CurAnswerTable.Rows[0];

   CurAnswerData.AnswerID = Convert.ToDecimal(CurAnswer[AnswerData.ANSWER_ID_FIELD]);
   CurAnswerData.AnswerState = Convert.ToDecimal(CurAnswer[AnswerData.ANSWER_STATE_FIELD]);
   CurAnswerData.AnswerMark = Convert.ToDouble(CurAnswer[AnswerData.ANSWER_MARK_FIELD]);
   //CurAnswerData.TotalTime = Convert.ToDecimal(CurAnswer[AnswerData.TOTAL_TIME_FIELD]);
   CurAnswerData.StartTime = Convert.ToDateTime(CurAnswer[AnswerData.START_DATETIME_FIELD]);
   CurAnswerData.LatestTime = Convert.ToDateTime(CurAnswer[AnswerData.LATEST_DATETIME_FIELD]);

   CurAnswerData.OperateStep = (byte[])(CurAnswer[AnswerData.OPERATE_STEP_FIELD]);

   CurAnswerData.AnswerDesc = Convert.ToString(CurAnswer[AnswerData.ANSWER_DESC_FIELD]);

   return true;
  }

  #endregion