本文介绍了我有一个上载程序控件,该图像已成功插入到db中,但无法检索它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些问题,请执行我没有使我的图像显示在gridview中,我没有遇到任何错误>>
httphandler(通用处理程序)>>
公共 class 处理程序:IHttpHandler { 公共 无效 ProcessRequest(HttpContext上下文){ SqlConnection con = 新 SqlConnection(); con.ConnectionString = " 数据源= HOME-PC \\ SQLEXPRESS;初始目录= imagek;集成安全性= True" ; con.Open(); SqlCommand命令= 新 SqlCommand(" 从图像中选择* ,骗局); SqlDataReader dr = command.ExecuteReader(); Dr.Read(); context.Response.BinaryWrite((字节 [])dr [ 0 ]); // context.Response.ContentType ="text/plain"; // context.Response.Write("Hello World"); con.Close(); context.Response.End(); } 公共 布尔 IsReusable { 获取 { 返回 假; } } }
默认页面>>
我有一个上载程序控件,该图像已成功插入db中,但无法检索它>>
sing 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.Data.SqlClient; using System.Windows.Forms; //using System.Web. public partial class _Default : System.Web.UI.Page { //con.ConnectionString =SqlConnection con = new SqlConnection(); public SqlConnection con = new SqlConnection(); protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True"; } protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { con.Open(); byte[] img = new byte[FileUpload1.PostedFile.ContentLength]; HttpPostedFile myimg = FileUpload1.PostedFile; myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength); SqlCommand cmd = new SqlCommand("insert into image values(22,''"+ img +"'')",con); cmd.ExecuteNonQuery(); con.Close(); } } protected void Button2_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); con.Open(); SqlCommand command = new SqlCommand("SELECT id from image ", con); // SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection); SqlDataAdapter daimages = new SqlDataAdapter(command); DataTable dt = new DataTable(); daimages.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); GridView1.Attributes.Add("bordercolor", "black"); } }
解决方案
hi i have some prob , wen i execute i dint get my image displayed in the gridview ,i aint getting any error>>
httphandler (generic handler)>>
public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True"; con.Open(); SqlCommand command = new SqlCommand("SELECT * from image ", con); SqlDataReader dr = command.ExecuteReader(); dr.Read(); context.Response.BinaryWrite((Byte[])dr[0]); //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); con.Close(); context.Response.End(); } public bool IsReusable { get { return false; } } }
default page>>
i have an uploader control , the image is inserted into the db successfully , but cant retrieve it>>
sing 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.Data.SqlClient; using System.Windows.Forms; //using System.Web. public partial class _Default : System.Web.UI.Page { //con.ConnectionString =SqlConnection con = new SqlConnection(); public SqlConnection con = new SqlConnection(); protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=imagek;Integrated Security=True"; } protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { con.Open(); byte[] img = new byte[FileUpload1.PostedFile.ContentLength]; HttpPostedFile myimg = FileUpload1.PostedFile; myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength); SqlCommand cmd = new SqlCommand("insert into image values(22,''"+ img +"'')",con); cmd.ExecuteNonQuery(); con.Close(); } } protected void Button2_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); con.Open(); SqlCommand command = new SqlCommand("SELECT id from image ", con); // SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection); SqlDataAdapter daimages = new SqlDataAdapter(command); DataTable dt = new DataTable(); daimages.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); GridView1.Attributes.Add("bordercolor", "black"); } }
解决方案
这篇关于我有一个上载程序控件,该图像已成功插入到db中,但无法检索它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!