简体   繁体   中英

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

I am working on asp.net with oracle database. 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.

Can any give me any suggestions ?

Here is my ImgHandler.ashx code ;

 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.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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