
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<HTML>
	<HEAD>
		<title>DisplayDBImageExample</title>
		
		<script language="C#" runat="server">
		
			void SubmitBtn_Click(object sender, System.EventArgs e)
			{
				int ImgID = Convert.ToInt32(ImgIDTextBox.Text);

				string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
				string query = "SELECT * FROM ImageTable WHERE ImageID = @ImageID";

				SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));
				myCommand.Parameters.Add("@ImageID", SqlDbType.Int);
				myCommand.Parameters["@ImageID"].Value = ImgID;
				myCommand.Connection.Open();

				SqlDataReader dr = myCommand.ExecuteReader();

				if(dr.Read())
				{
					Response.ContentType = (string)dr["ImageContentType"];
					Response.OutputStream.Write((byte[])dr["ImageData"], 0, (int)dr["ImageSize"]);
				}
				else
				{
					Response.Write("没有这个图片的ID号");
					Response.End();
				}

				dr.Close();
				myCommand.Connection.Close();
			}
		
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>将数据库保存的图片显示到页面上示例</h3>
			输入图片在数据库中的ID：
			<asp:textbox id="ImgIDTextBox" runat="server"></asp:textbox>
			<asp:button id="SubmitBtn" runat="server" Text="显示" OnClick="SubmitBtn_Click"></asp:button>
		</form>
	</body>
</HTML>
