2

可能重复:
为什么在这个 CLOB 字段的 GetOrdinal 函数中出现 OutOfRange 异常?

I need to read a CLOB field from an ORACLE table int a C# variable of type String. Does anyone know how to accomplish this task? 

这就是我所做的,但是在计算字段的 GetOrdinal 时我得到了 IndexOutofRange。提前致谢。

 public void ReadFunction(string FName, out string fContent) 
{ 
    OracleCommand command = _connection.CreateCommand(); 
    OracleTransaction transaction = _connection.BeginTransaction(); 
    command.Transaction = transaction; 
    command.CommandText = "SELECT TO_CLOB(TO_NCLOB(FUNCTION_SCRIPT)) FROM IS_FUNCTION where   FNAME=:fName "; 
    command.Parameters.Add("FName", OracleType.NVarChar).Value = FName; 
    OracleDataReader odr = command.ExecuteReader(); 
    int temp = odr.GetOrdinal("FUNCTION_SCRIPT"); 
    OracleLob myLob = odr.GetOracleLob(temp); 
    fContent = (String)myLob.Value; 
    odr.close(); 
} 
4

1 回答 1

6

这是获取 Blob 的代码,然后您应该按照您需要的方式将其转换为字符串。我不知道你需要的格式。

  // create and open connection
  // change for your environment
  string connStr = "User Id=pm; Password=pm; Data Source=orcllx; Pooling=false";
  OracleConnection con = new OracleConnection(connStr);
  try
  {
    con.Open();
  }
  catch (OracleException ex)
  {
    MessageBox.Show(ex.Message);
  }

  // statement to get a blob
  string sql = "select ad_composite from print_media where product_id=3106 and
               ad_id=13001";

  // create command object
  // InitialLOBFetchSize
  //  defaults to 0
  OracleCommand cmd = new OracleCommand(sql, con);

  cmd.InitialLOBFetchSize = 8192;

  // create a datareader
  OracleDataReader dr = cmd.ExecuteReader();

  // read the single row result
  try
  {
    dr.Read();
  }
  catch (OracleException ex)
  {
    MessageBox.Show(ex.Message);
  }

  // use typed accessor to retrieve the blob
  OracleBlob blob = dr.GetOracleBlob(0);

  // create a memory stream from the blob
  MemoryStream ms = new MemoryStream(blob.Value);

  // set the image property equal to a bitmap
  // created from the memory stream
  pictureBox1.Image = new Bitmap(ms);
于 2012-09-25T12:58:35.653 回答