1

我想完成类似的事情:

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

现在,我将在 Crystal Report 中填充 temp.xsd,以便动态显示图像。这只是我从头开始编写的示例代码,但为了适应我的场景,我需要获取已经存在的图像,dtAcctSigner.Rows[0]["IMAGE_BLOB"],所以我想知道是否有任何方法可以获取这个 BLOB,就像我在上面的代码中获取一样

OracleDataReader.GetOracleLob();

为此,我需要将数据表(Type-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'

请让我知道我做错了什么。

4

1 回答 1

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);
于 2012-08-27T22:05:46.507 回答