如何将CLOB字段读入C#变量?

时间:2012-09-24 23:52:03

标签: c# sql oracle select

  

可能重复:
  Why Do I get OutOfRange Exception in GetOrdinal Function of this CLOB field?

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(); 
} 

1 个答案:

答案 0 :(得分:4)

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

  // 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);