asp.net存储图片到sql server数据库并显示
2010-11-09 13:03:06 来源:WEB开发网核心提示:SQL Server数据库简体繁体数据混用的问题http://tech.cncms.com/shujuku/mssql/100037.htmlSQL Server数据库对于应用程序的关系UploadImage.aspx文件<%@ Page Language="C#" AutoEventWire
http://tech.cncms.com/shujuku/mssql/100037.html
UploadImage.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadImage.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>龙振升(爽爽)QQ:749081771_www.15597.com</title> </head> <body> <form id="form1" runat="server"> <div> <TABLE RUNAT="server" WIDTH="700" ALIGN="left" ID="Table1" cellpadding="0" cellspacing="0" border="0"> <TR> <TD>上传图片(选择你要上传的图片)</TD> <TD> <asp:FileUpload ID="UP_FILE" runat="server" /> </TD> </TR> <TR> <TD> </TD> <TD> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></TD> </TR> </TABLE> </div> </form> </body> </html>
UploadImage.aspx.cs文件
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Int32 FileLength = UP_FILE.PostedFile.ContentLength; Byte[] myByteArray = new Byte[FileLength]; Stream myStream = UP_FILE.PostedFile.InputStream; myStream.Read(myByteArray, 0, FileLength); SqlConnection conn = new SqlConnection("server=.;database=testdb;uid=sa;pwd=698887;"); conn.Open(); String sqlcmd = "insert into ImageStore(ImageData) values(@Image)"; SqlCommand CmdObj = new SqlCommand(sqlcmd, conn); SqlParameter ImagePara = new SqlParameter(); ImagePara.ParameterName = "@Image"; ImagePara.SqlDbType = SqlDbType.Image; ImagePara.Value = myByteArray; CmdObj.Parameters.Add(ImagePara); CmdObj.ExecuteNonQuery(); conn.Close(); myStream.Close(); } }
ReadImage.aspx.cs文件
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; using System.Drawing; public partial class ReadImage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=testdb;uid=sa;pwd=698887;"); conn.Open(); String sqlcmd = "select * from ImageStore where ImageID=7"; SqlCommand cmd = new SqlCommand(sqlcmd,conn); SqlDataReader sqlReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); sqlReader.Read(); byte[] ImageData = (byte[])sqlReader["ImageData"]; Response.BinaryWrite(ImageData); Response.End(); conn.Close(); } }
Default.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>爽爽-7wy.net</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Image ID="Image1" runat="server" Height="115px" ImageUrl="~/ReadImage.aspx" Width="220px" /> </div> </form> </body> </html>
- ››asp.net页面弄成伪静态页面
- ››Asp.net 中将汉字转换成拼音的方法
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
赞助商链接