从数据库检索/显示BLOB [英] Retrieving/Displaying BLOB from Database

查看:375
本文介绍了从数据库检索/显示BLOB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我试图在asp.net页中显示一个Word文档,该文档已作为OleDb对象保存在Access数据库2003中.如果我没有记错的话,该过程与从数据库中检索和显示图像的过程相同.我在网上找到了一段代码来检索该代码,并试图对其进行研究以了解其工作原理.通过查看代码,我可以理解以下内容:

1使用sql语句从数据库中获取要显示的文件的ID
2使用sql语句获取所需文件的内容类型
3使用SqlCommand类的ExecuteScalar()方法读取BLOB值.
4将返回对象强制转换为字节数组,并将其存储在字节数组中,因为
executeScalar()返回一个对象.
5将其流回HTTP响应

以下是我尝试使用的代码段:

Hi all, I''m trying to display a word document in my asp.net page which had been saved in Access database 2003 as an OleDb Object. If I''m not mistaken the process is the same as retrieving and displaying an image from the database. I''ve found a snippet of code online for retrieving doing just that and have tried to study it to understand how it works. This is what I understand from looking at the code:

1 Grab the id of the file you want to display from the database using a sql statement
2 Grab the content type of the file you want using a sql statement
3 Read the BLOB value using the ExecuteScalar() method of the SqlCommand class.
4 Cast the return object to byte array and store it in byte array because
executeScalar() returns an object.
5 Stream it back to the HTTP response

The following is the code snippet I''m trying to use:

void  CreateImage(string id)
{
    // Connectoin string is taken from web.config file.
    SqlConnection _con = new SqlConnection(
      System.Configuration.ConfigurationSettings.AppSettings["DB"]);
        
    try
    {
        _con.Open();
        SqlCommand _cmd = _con.CreateCommand();
        _cmd.CommandText = "select logo from" + 
                           " pub_info where pub_id='" + 
                           id + "'";
        byte[] _buf = (byte[])_cmd.ExecuteScalar();
        
        // This is optional
        Response.ContentType = "image/gif";
        
        //stream it back in the HTTP response
        Response.BinaryWrite(_buf);                
                
    }
    catch
    {}
    finally
    {
        _con.Close();
                
    }
}



在Page_Load()中,调用CreateImage方法.




In the Page_Load(), call CreateImage method.


private void Page_Load(object sender, System.EventArgs e)
{
    if(!IsPostBack)
    {
        CreateImage(Request["id"]);
    }
}


然后使用


Then use

<img src='<%# "imgs.aspx?id=" + drpIds.SelectedValue %>'>

以显示图像

最后调用

to display image

Finally call

Page.databind.



我的问题是如何修改



My question is how can I modify the step

<img src='<%# "imgs.aspx?id=" + drpIds.SelectedValue %>'>

在文本字段中显示单词doc .在此先感谢您的帮助.

to display a word doc in a text field. Thanks in advance for your help.

推荐答案

对于类似问题,请在下面查看我的回复.

sql server 2008 BLOB [
Please have a look at my response below for similar kind of question.

sql server 2008 BLOBs[^]

I have attached sample solution in the link.


按照此
Follow this link
it is discussed there how this can be done and also its alternative.


这篇关于从数据库检索/显示BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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