传递OracleLob作为参数的函数 [英] Passing OracleLob as parameter to a function

查看:136
本文介绍了传递OracleLob作为参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成类似的事情:

 尝试
{
    的openConnection();
    的OracleCommand CMD =新的OracleCommand(选择CUSTOMER_ID,IMAGE_BLOB从CUSTOMER_IMAGE WHERE CUSTOMER_ID IN(3026),康涅狄格州);
    字符串的PubID =;
    OracleDataReader读卡器= cmd.ExecuteReader(CommandBehavior.SequentialAccess也);

    //创建报表对象
    MemoryStream的memStream;
    的DataSet ds为新的DataSet();
    数据表ImageTable =新的DataTable();
    BinaryReader在binReader;
    DataRow的博士;

    byte []的byteArrName;
    OracleLob斑点;

    而(reader.Read())
    {
        的PubID = reader.GetValue(0)的ToString();

        //获取LOB
        斑点= reader.GetOracleLob(1);

        //创建的斑点所获得的大小的字节数组
        byteArrName =新的字节[blob.Length]

        //读取BLOB数据到字节数组
        INT I = blob.Read(byteArrName,0,System.Convert.ToInt32(blob.Length));

        //复制字节数组的内容流
        memStream =新的MemoryStream(byteArrName);

        //创建类型字节柱[]
        ImageTable.Columns.Add(新的DataColumn(ID的typeof(串)));
        ImageTable.Columns.Add(新的DataColumn(形象的typeof(System.Byte [])));

        //读取其中有BLOB数据的流
        binReader = BinaryReader在新(memStream);
        博士= ImageTable.NewRow();

        博士[ID] = PubID的;
        //的ReadBytes方法添加的图像流的字节数组。
        博士[形象] = binReader.ReadBytes((INT)binReader.BaseStream.Length);
        ImageTable.Rows.Add(DR);

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

    ds.Tables.Add(ImageTable);

    //创建一个临时的数据集而保存图像
    ds.WriteXmlSchema(@ Directory.GetCurrentDirectory()+\\ temp.xsd);

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

现在,我将填充在Crystal报表temp.xsd使得图像将动态显示。这仅仅是一个样品code我从头开始写的,但适合我的方案,我需要的是已经在 dtAcctSigner.Rows [0] [IMAGE_BLOB] 所以只是想知道是否有什么办法可以获取这个BLOB就像我取上述code为

  OracleDataReader.GetOracleLob();
 

对于这一点,我需要的数据表(类型OracleLob)作为参数的一列传递给这样的功能:

 更新(dtAcctSigner.Rows [0] [IMAGE_BLOB]);
 

和功能的情况如下:

 公共无效更新(OracleLob一)
{
//我想做采取OracleLob,并到MemoryStream,放入这里temp.xsd
}
 

不过,我得到一个错误:

 不能将对象到OracleLob
 

请让我知道我做错了。

解决方案

 使用(阅读器)
{
      //获取数据的第一行。
      reader.Read();
      //获取的LOB(全部3个品​​种)。
      OracleLob BLOB = reader.GetOracleLob(1);
      ...

      //例 - 读二进制数据(块)。
      byte []的缓冲区=新的字节[4096];
      而((实际= BLOB.Read(缓冲液,0,buffer.Length))大于0)
         Console.WriteLine(BLOB.LobType +
            .Read(+缓冲液+,+ buffer.Length +)=>中实际+);

      ...
}
 

我个人创建并添加列到DataTable这种方式,但它是由你来试试吧,你知道会以这种方式工作,或另一种方式

 数据表表=新的DataTable(ImageTable); //创建一个新的DataTable实例。

DataColumn的column0 =新的DataColumn(ID); //创建列。
column.DataType = System.Type.GetType(System.String); //类型的字符串

DataColumn的列1 =新的DataColumn(图像); //创建列。
column.DataType = System.Type.GetType(System.Byte []); //类型byte []存储图像的字节数。
column.AllowDBNull = TRUE;
column.Caption =我的图片;

table.Columns.Add(column0); //列添加到表中。
table.Columns.Add(列1); //列添加到表中。

然后,一个新的行添加到该表,并设置MYIMAGE列的值。

DataRow的行= table.NewRow();
行[MYIMAGE] =<图像的字节数组取代;
tables.Rows.Add(行);
 

I want to accomplish a similar thing:

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

Now, I'll populate that temp.xsd in the Crystal Report such that the image will be displayed dynamically. This is just a sample code I wrote from scratch, but to fit my scenario, I need to get the image that is already in dtAcctSigner.Rows[0]["IMAGE_BLOB"], so just wondering if there's any way I can fetch this BLOB just like I fetch in the above code as

OracleDataReader.GetOracleLob();

For that, I need to pass a column of the datatable(Type-OracleLob) as a parameter to a function like this:

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

And the function goes as follows:

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

But I get an error:

cannot convert 'object' to 'OracleLob'

Please let me know what I'm doing wrong.

解决方案

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

      ...
}

I personally would create and add columns to the DataTable this way but it's up to you to try it this way or another way that you know will work

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

这篇关于传递OracleLob作为参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆