<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="Server">
Dim objRs As SqlDataReader
Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Dim objDb As New SqlConnection("Data Source=(local);User ID=sa;Password=sa;Persist Security Info=True;Initial Catalog=dotnet")
' image_dataテーブルから取り出したファイル情報を
' DropDownListコントロールにバインド
Dim objCom As New SqlCommand("SELECT id,title FROM image_data",objDb)
objDb.Open()
objRs=objCom.ExecuteReader()
Page.DataBind()
objDb.Close()
End If
End Sub
Sub objBtn_Click(sender As Object, e As EventArgs)
Dim objDb As New SqlConnection("Data Source=(local);User ID=sa;Password=sa;Persist Security Info=True;Initial Catalog=dotnet")
' ドキュメントIDをキーに
' DropDownListコントロールで指定されたファイルを取得
Dim objCom As New SqlCommand("SELECT type,datum FROM image_data WHERE id=@id",objDb)
objCom.Parameters.Add("@id",objDdp.SelectedItem.Value)
objDb.Open()
Dim objRs=objCom.ExecuteReader()
' データベースから取得したファイルを出力する。
' typeフィールドの値をContentTypeプロパティにセットし、
' datumフィールドの値をBinaryWriteメソッドで出力する。
' それぞれ取得したフィールド値はCType関数で型変換する必要がある。
If objRs.Read() Then
Response.ContentType=CType(objRs.Item(0),String)
Response.BinaryWrite(CType(objRs.Item(1),Byte()))
End If
objDb.Close()
' 出力の終了
Response.End()
End Sub
</script>
<html>
<head>
<title>バイナリデータの出力</title>
</head>
<body>
<form runat="Server">
出力データ:
<asp:DropDownList id="objDdp" runat="Server"
DataSource="<%#objRs%>" DataTextField="title" DataValueField="id" />
<asp:Button id="objBtn" runat="Server"
Text="出力" OnClick="objBtn_Click" />
</form>
</body>
</html>