2012-08-27 81 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。 –

相关问题