下面举一个从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> |
关键字词: