通过ADO.NET读取并显示数据库中的图片

作者:网络 来源:佚名 更新时间:2008-09-25 02:58:14 点击:

下面举一个从microsoft sql server的pub数据库读取图片并显示它的例子:

以下是示例源代码:
<%@ import namespace="system.data.sqlclient" %> 
<%@ import namespace="system.drawing" %> 
<%@ import namespace="system.drawing.imaging" %> 
<%@ import namespace="system.io" %> 
<script language="c#" runat="server"> 
void page_load(object sender, eventargs e) {
        memorystream stream = new memorystream();
        sqlconnection connection;
        connection = new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=");
        try {
            connection.open();
            sqlcommand command;
            command = new sqlcommand("select logo from pub_info where pub_id=\’0736\’", connection);
            byte[] image;
            image = command.executescalar();
            stream.write(image, 0, image.length);
            bitmap imgbitmap;
            imgbitmap = new bitmap(stream);
            response.contenttype = "image/gif";
            imgbitmap.save(response.outputstream, imageformat.gif);
        }
        finally {
            connection.close();
            stream.clse();
        }
    }

</script>