2012-09-24 1 views
2

가능한 중복은 :
Why Do I get OutOfRange Exception in GetOrdinal Function of this CLOB field?CLOB 필드를 C# 변수로 읽는 방법?

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

음? http://stackoverflow.com/questions/12574341/why-do-i-get-outofrange-exception-in-getordinal-function-of-this-clob-field –

+0

도움이 필요 심하게 생각하고 제 질문의 제목을 개선해야 할 필요가 있다고 생각했습니다. – user1298925

+2

@ user1298925 그러면 원래 질문의 제목을 편집해야합니다. – phoog

답변

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); 
+0

dr.Read()가 누락 된 링크입니다. . 측면에서와 마찬가지로 SQL Select 문에 데이터를 포함하면 SQL Injection Attack으로 간주되어 피하는 것이 좋습니다. 너와 그 지식을 나눌 줄 알았다. 희망은 유용합니다. – user1298925

+0

'InitialLOBFetchSize'를 설정할 필요가 없었습니다. 아마도 필요하지는 않지만 나쁜 옵션은 아닙니다. blob을 바이트 배열로 가져 오는 것은 실제로 일반적입니다.이 작업은'byte [] array = new byte [blob.Length]; blob.Read (array, 0, (int) array.Length);'. 그리고 정말로, if (dr.HasRows) {...}'조건을 사용해야하며, 그 다음에 BLOB가 검색되고 그 모든 것이 내부로 들어갑니다. WPF에서는 새로운 BitmapImage를 만들고 위에서와 같이'StreamSource'에'MemoryStream'을 할당해야합니다. 그러나 ('BitmapCreateOptions, BitmapCacheOption') 선언 할 몇 가지 옵션이 있습니다. – vapcguy

관련 문제