using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.IO;
using System.Data.SqlClient;namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myDSN = new SqlConnection (@"Data Source=PC2011030210OVW;Initial Catalog=GJmanagementSystem;Integrated Security=True");//new SqlConnection(Application["Test_Conn"].ToString());
myDSN.Open();
int imgid = 12; //int.Parse(Request.QueryString["11"]);
string sqlText = "SELECT images FROM Images where id=" + imgid;
Trace.Write(sqlText);
SqlCommand MyCommand = new SqlCommand(sqlText, myDSN);
SqlDataReader dr = MyCommand.ExecuteReader();
///////////////////////////////
SqlCommand CmdObj = new SqlCommand(sqlText, myDSN);
CmdObj.Parameters.Add("@id", SqlDbType.Int).Value =imgid;
//Con.Open();
// SqlDataReader SqlReader = CmdObj.ExecuteReader();
//SqlReader.Read();
////////////////////////////////
if (dr.Read())
{
// Response.ContentType = (dr["img_contenttype"].ToString());
Response.BinaryWrite((byte[])dr["images"]);
}
myDSN.Close();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.IO;
using System.Data.SqlClient;namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myDSN = new SqlConnection (@"Data Source=PC2011030210OVW;Initial Catalog=GJmanagementSystem;Integrated Security=True");//new SqlConnection(Application["Test_Conn"].ToString());
myDSN.Open();
int imgid = 12; //int.Parse(Request.QueryString["11"]);
string sqlText = "SELECT images FROM Images where id=" + imgid;
Trace.Write(sqlText);
SqlCommand MyCommand = new SqlCommand(sqlText, myDSN);
SqlDataReader dr = MyCommand.ExecuteReader();
///////////////////////////////
SqlCommand CmdObj = new SqlCommand(sqlText, myDSN);
CmdObj.Parameters.Add("@id", SqlDbType.Int).Value =imgid;
//Con.Open();
// SqlDataReader SqlReader = CmdObj.ExecuteReader();
//SqlReader.Read();
////////////////////////////////
if (dr.Read())
{
// Response.ContentType = (dr["img_contenttype"].ToString());
Response.BinaryWrite((byte[])dr["images"]);
}
myDSN.Close();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
2Response.BinaryWrite((byte[])dr["images"]);这样是无法显示图片的,倒是可以提供图片下载
显示图片你需要将(byte[])dr["images"]写到硬盘中,然后使用类似Response.write("<ima src='location'>")方式解决.
if (this.FileUpload1.HasFile)
{
string strPath = FileUpload1.FileName;
try
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1);
FileUpload1.SaveAs.SaveAs(Server.MapPath("../UploadFile/")+fileName + extension);
strPath = fileName + extension;
using(SqlConnection conn =new SqlConnection(""))
{
conn .Open();
string sql = "";
SqlCommand cmd = new SqlCommand(sql, conn );
cmd.ExecuteNonQuery();
conn.Close();
} }
catch()
{
}
}
// 一般保存路径二进制使用
Stream s = FileUpload1.PostedFile.InputStream;
Byte[] buffer= new Byte[FileUpload1.PostedFile.ContentLength];
s.Read(buffer,0,FileUpload1.PostedFile.ContentLength);
string strsql = "insert into Tempimage(images,names)values(@ImageData,@names)";
SqlCommand cmd= new SqlCommand(strsql, conn);
cmd.Parameters.Add("@ImageData", SqlDbType.Image);
cmd.Parameters.Add("@names", SqlDbType.VarChar);
cmd.Parameters["@ImageData"].Value = buffer;
cmd.Parameters["@names"].Value = "";
cmd.ExecuteNonQuery();//显示
Image1.ImageUrl="Photo.aspx?id="+Request.QueryString["id"];
photo.aspx
int Id=Request.QueryString["id"]==null?0:int.Parse(Request.QueryString["id"].ToString());
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from A where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
}
if (!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql = "select * from test where id=3";
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Pic"]);
Response.End();
reader.Close();
//Image image = Image.FromStream(ms, true);
//imgPhoto.ImageUrl = "Default.aspx";
}