プログラムのメモ置き場 @ ウィキ
asp006
最終更新:
chaka-poko
-
view
バイナリ・データを表示する
テーブルのレイアウト
フィールド名 | データ型 | |
id | int | ID(主キー 連番) |
subject | varchar(300) | 件名 |
title | varchar(100) | ファイル名 |
type | varchar(50) | MIMEタイプ |
idata | varbinary(MAX) | バイナリ・データ |
last_modified | datetime | 最終更新日 |
ImageShow.aspx
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>イメージの表示</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <h3>
- データベースに登録したバイナリデータを表示する</h3>
- <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
- DataTextField="title" DataValueField="id">
- </asp:DropDownList>
- <asp:Button ID="Button1" runat="server" Text="表示" />
- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NetWorks01ConnectionString %>"
- SelectCommand="SELECT [id], [title] FROM [d_Image]"></asp:SqlDataSource>
- </form>
- </body>
- </html>
ImageShow.aspx.vb
- Imports System.Data.SqlClient
- Imports System.Data
-
- Partial Class ImageShow_ImageShow
- Inherits System.Web.UI.Page
-
- Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
-
- Dim id As Integer
- id = Me.DropDownList1.SelectedValue
-
- Dim db As New SqlConnection(ConfigurationManager.AppSettings("dbstr"))
- Dim cmd As SqlCommand = New SqlCommand("SELECT type,idata FROM d_Image WHERE id=@p1", db)
- Dim p1 As SqlParameter = cmd.Parameters.Add("@p1", SqlDbType.Int)
- p1.Value = id
- db.Open()
- Dim rs As SqlDataReader = cmd.ExecuteReader()
- If rs.Read Then
- Response.ContentType = CType(rs("type"), String)
- Response.BinaryWrite(DirectCast(rs("idata"), Byte()))
- Response.End()
- End If
- rs.Close()
- db.Close()
-
- End Sub
- End Class
参考にさせていただいたページ
[Counter] Total: -