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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12574508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:07:45  来源:igfitidea点击:

How to read a CLOB field into a C# variable?

c#sqloracleselect

提问by user1298925

Possible Duplicate:
Why Do I get OutOfRange Exception in GetOrdinal Function of this CLOB field?

可能的重复:
为什么我在这个 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? 

This is what I have done but I am getting an IndexOutofRange when calculating the GetOrdinal of the field. Thanks in advance.

这就是我所做的,但是在计算该字段的 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(); 
} 

回答by NicoRiff

This is the code to get the Blob, then you should cast it as astring in the way you need it. I do not know the format you need.

这是获取 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);