简体   繁体   English

如何检查BLOB字段是否具有数据?

[英]How do I check a BLOB field has Data or not?

I am working on asp.net with oracle database. 我正在使用oracle数据库在asp.net上工作。 I want to print the image of employee which is saved in an old table. 我要打印保存在旧表中的雇员图像。 I don't even the data type of image saved in the photo field of that table. 我什至没有保存在该表的照片字段中的图像数据类型。

I used Image handlers to print the images from newly created table but when I query on old tables the images is not getting printed. 我使用图像处理程序从新创建的表中打印图像,但是当我在旧表上查询时,图像并没有被打印。

How do I know that is there any image saved in the table? 我怎么知道表格中保存了任何图像?

If there is an image why it's not getting printed? 如果有图像,为什么不打印?

I'll show you the code for image handler for both the table (NEW , OLD) Image from newly created table is printing very fine but what's the problem in old one. 我将向您展示这两个表(NEW,OLD)的图像处理程序的代码。新创建的表中的图像可以很好地打印,但是旧表中的问题是什么?

Can any give me any suggestions ? 能给我任何建议吗?

Here is my ImgHandler.ashx code ; 这是我的ImgHandler.ashx代码;

 public void ProcessRequest (HttpContext context)
    {
        OracleDataReader rdr = null;
        OracleConnection conn = Conn.getConn();
        OracleCommand cmd = null;
        string ImgType = context.Request.QueryString["typ"].ToString();
        try
        {
            if (ImgType=="edu")//this is working fine
            {
                cmd = new OracleCommand("select attachment pic from newtbl where lvldcp_code=" + context.Request.QueryString["dcp"] + "and emp_code="+context.Request.QueryString["emp"], conn);
            }
            else if (ImgType=="profile")
            {
                cmd = new OracleCommand("select photo pic from oldtbl where emp_code="+context.Request.QueryString["emp"], conn);
            }

            conn.Open();
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite((byte[])rdr["pic"]);
            }
            if (rdr != null)
                rdr.Close();
        }
        catch (Exception ex)
        {

        }
        finally
        {
            if (conn != null)
                conn.Close();
        }


    }

If your queries are returning a blob field value then you could use the OracleBlob class. 如果查询返回的是Blob字段值,则可以使用OracleBlob类。

public void ProcessRequest (HttpContext context)
{
    OracleDataReader rdr = null;
    OracleConnection conn = Conn.getConn();
    OracleCommand cmd = null;
    string ImgType = context.Request.QueryString["typ"].ToString();
    try
    {
        if (ImgType=="edu")//this is working fine
        {
            cmd = new OracleCommand("select attachment pic from newtbl where lvldcp_code=" + context.Request.QueryString["dcp"] + "and emp_code="+context.Request.QueryString["emp"], conn);
        }
        else if (ImgType=="profile")
        {
            cmd = new OracleCommand("select photo pic from oldtbl where emp_code="+context.Request.QueryString["emp"], conn);
        }

        Byte[] byteArray = null;
        OracleBlob blob;
        conn.Open();
        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            blob = rdr.GetOracleBlob(0);
            byteArray = new Byte[blob.Length];
            int i = blob.Read(byteArray, 0, System.Convert.ToInt32(blob.Length));

            //clob.Length or i > 0 will show if there are bites in the clob or not
        }
        if (rdr != null)
            rdr.Close();

        context.Response.ContentType = "image/jpg";
        context.Response.BinaryWrite(byteArray);
    }
    catch (Exception ex)
    {

    }
    finally
    {
        if (conn != null)
            conn.Close();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM