2012-08-27 82 views
1

我要完成類似的事情:傳遞OracleLob作爲參數傳遞給函數

try 
{ 
    openConnection(); 
    OracleCommand cmd = new OracleCommand("SELECT CUSTOMER_ID, IMAGE_BLOB FROM CUSTOMER_IMAGE WHERE CUSTOMER_ID IN (3026)", conn); 
    string pubID = ""; 
    OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); 

    //create the report object 
    MemoryStream memStream; 
    DataSet ds = new DataSet(); 
    DataTable ImageTable = new DataTable(); 
    BinaryReader binReader; 
    DataRow dr; 

    byte[] byteArrName; 
    OracleLob blob; 

    while (reader.Read()) 
    { 
     pubID = reader.GetValue(0).ToString(); 

     // Obtain a LOB 
     blob = reader.GetOracleLob(1); 

     // Create a byte array of the size of the Blob obtained 
     byteArrName = new byte[blob.Length]; 

     // Read blob data into byte array 
     int i = blob.Read(byteArrName, 0, System.Convert.ToInt32(blob.Length)); 

     //Copied the contents of byte array to stream 
     memStream = new MemoryStream(byteArrName); 

     //Create a column of type byte[] 
     ImageTable.Columns.Add(new DataColumn("id", typeof(string))); 
     ImageTable.Columns.Add(new DataColumn("image", typeof(System.Byte[]))); 

     //Reading the stream which has the blob data 
     binReader = new BinaryReader(memStream); 
     dr = ImageTable.NewRow(); 

     dr["id"] = pubID; 
     //ReadBytes method to add a byte array of the image stream. 
     dr["image"] = binReader.ReadBytes((int)binReader.BaseStream.Length); 
     ImageTable.Rows.Add(dr); 

     memStream.Close(); 
     binReader.Close(); 
    } 

    ds.Tables.Add(ImageTable); 

    //Creating a temporary dataset which hold the image 
    ds.WriteXmlSchema(@Directory.GetCurrentDirectory() + "\\temp.xsd"); 

    reader.Close(); 
    conn.Close(); 
} 

現在,我將填充在水晶報告認爲temp.xsd使得圖像將動態顯示。這只是我從頭開始編寫的示例代碼,但爲了適應我的情況,我需要獲取已在dtAcctSigner.Rows[0]["IMAGE_BLOB"],中的圖像,以便想知道是否有任何方法可以獲取此BLOB,就像我在上面的代碼中獲取的那樣,代碼爲

OracleDataReader.GetOracleLob(); 

對於這一點,我需要的數據表(類型OracleLob)的列作爲參數傳遞給這樣的功能:

Update(dtAcctSigner.Rows[0]["IMAGE_BLOB"]); 

和函數去如下:

public void Update(OracleLob a) 
{ 
// I want to do take the OracleLob and make into a memorystream and put it into temp.xsd here 
} 

但我得到一個錯誤:

cannot convert 'object' to 'OracleLob' 

請讓我知道我做錯了什麼。

+0

你需要清理你的問題那麼..也不要發佈空白的代碼方法..如果我們要幫助你,你必須對你遇到的問題更加清楚/具體..這有點混亂/很難請遵循你正在努力完成的任務 請在你如何傳遞Update(dtAcctSigner.Rows [iCount1] [「SIGNATURE_BLOB」])上顯示完整的代碼; 以及聲明數據表 – MethodMan

+0

好的謝謝。將編輯代碼。 –

+0

大多數人在下列情況下投票結果如下 1.沒有顯示出足夠的個人研究 2.非常抽象的本質 3.是不是一個真正的問題..等 – MethodMan

回答

0
using(reader) 
{ 
     //Obtain the first row of data. 
     reader.Read(); 
     //Obtain the LOBs (all 3 varieties). 
     OracleLob BLOB = reader.GetOracleLob(1); 
     ... 

     //Example - Reading binary data (in chunks). 
     byte[] buffer = new byte[4096]; 
     while((actual = BLOB.Read(buffer, 0, buffer.Length)) >0) 
     Console.WriteLine(BLOB.LobType + 
      ".Read(" + buffer + ", " + buffer.Length + ") => " + actual); 

     ... 
} 

我個人創建並添加列到DataTable這種方式,但它是由你來嘗試一下這種方式或其他方式,你知道將工作

DataTable table = new DataTable("ImageTable"); //Create a new DataTable instance. 

DataColumn column0 = new DataColumn("id"); //Create the column. 
column.DataType = System.Type.GetType("System.String"); //Type string 

DataColumn column1 = new DataColumn("image"); //Create the column. 
column.DataType = System.Type.GetType("System.Byte[]"); //Type byte[] to store image bytes. 
column.AllowDBNull = true; 
column.Caption = "My Image"; 

table.Columns.Add(column0); //Add the column to the table. 
table.Columns.Add(column1); //Add the column to the table. 

Then, add a new row to this table and set the value of the MyImage column. 

DataRow row = table.NewRow(); 
row["MyImage"] = <Image byte array>; 
tables.Rows.Add(row); 
+0

這段代碼可以幫助我瞭解大塊。但是我將它轉換爲內存流是因爲我應該在運行時將圖像寫入CrystalReports中。 –

+0

噢,好吧我沒有意識到這一點..我要添加另一個例子,我將如何創建DataTable,並將下面的列分配給我有 – MethodMan

+0

以上的答案我將它放入內存流中並將其寫入臨時XML模式在硬盤上,以便它可以填充到CrystalReports中。另外,爲了使用reader.GetOracleLob,我沒有使用OracleDataReader,而是在Datatable中使用了這個BLOB對象。所以,我需要知道如何在沒有OracleDataReader的情況下將數據表中的列轉換爲oraclelob。 –

相關問題