将OracleLob作为参数传递给函数

时间:2012-08-27 21:08:01

标签: c# .net oracle ado.net blob

我想完成类似的事情:

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'

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

1 个答案:

答案 0 :(得分: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);