繁体   English   中英

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

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

我正在使用oracle数据库在asp.net上工作。 我要打印保存在旧表中的雇员图像。 我什至没有保存在该表的照片字段中的图像数据类型。

我使用图像处理程序从新创建的表中打印图像,但是当我在旧表上查询时,图像并没有被打印。

我怎么知道表格中保存了任何图像?

如果有图像,为什么不打印?

我将向您展示这两个表(NEW,OLD)的图像处理程序的代码。新创建的表中的图像可以很好地打印,但是旧表中的问题是什么?

能给我任何建议吗?

这是我的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();
        }


    }

如果查询返回的是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