html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

通过ADO.NET读取并显示数据库中的图片_.NET教程_编程技术

[ ] 已经帮助:人解决问题

下面举一个从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>

iABHTML5中文学习网 - HTML5先行者学习网
iABHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助