与存储和检索数据库中的图像有关[英] Related to storing and retriving image from database

问题描述

数据库存储和检索图像的问题请帮助..
检索.aspx

<asp:SqlDataSource ID="SqlDataSource1" runat="服务器"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT [ImageName], [Image], [STUDENT_ID], [FNAME] FROM [STUDENT] ORDER BY [STUDENT_ID]"></asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1"onselectedindexchanged="GridView1_SelectedIndexChanged"><列><asp:BoundField DataField="ImageName" HeaderText="ImageName"SortExpression="ImageName"/><asp:TemplateField HeaderText="图片"><项目模板><asp:Image runat="服务器" id="image1" ImageUrl='<%# Eval("ID","Handler.ashx?ID={0}") + Eval("ID")%>'/></ItemTemplate></asp:TemplateField><asp:BoundField DataField="STUDENT_ID" HeaderText="STUDENT_ID" ReadOnly="True"SortExpression="STUDENT_ID"/><asp:BoundField DataField="FNAME" HeaderText="FNAME" SortExpression="FNAME"/></></asp:GridView>处理程序.asax<%@ WebHandler 语言="C#" ="Handler" %>使用系统;使用 System.Web;使用 System.Data.SqlClient;使用 System.Configuration;public class 处理程序:IHttpHandler {public void ProcessRequest(HttpContext context){SqlDataReader dr = null;SqlConnection con = null;SqlCommand cmd = null;试试{con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);cmd.CommandText = "select IMAGENAME,IMAGE from STUDENT where ID=@ID";cmd.CommandType = System.Data.CommandType.Text;cmd.Connection = con;SqlParameter ImageID = new SqlParameter("@ID",System.Data.SqlDbType.Int);ImageID.Value = context.Request.QueryString["ID"];cmd.Parameters.Add(ImageID);con.Open();博士 = cmd.ExecuteReader();博士读();context.Response.BinaryWrite((byte[])dr["图片"]);博士关闭();con.Close();}catch(例外){}}public bool IsReusable {get {return false;}}string strImageName = FileUpload1.FileName.ToString();if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName == ""){byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];HttpPostedFile uploadimage = FileUpload1.PostedFile;uploadimage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);String sr = "插入STUDENT(ImageName,Image,STUDENT_ID,FNAME,MNAME,LASTNAME,MOTHERNAME,LOCALADDRESS,DISTRICT,STATE,PINCODE,PERMENANTADDRESS,DISTRICT1,STATE1,PINCODE1,LANDLINENO,MOBILENO,EMAIL_ID,SEX,DATEOFBIRTH) 值(@ImageName,@Image,'"+STUDENT_ID.Text.ToUpper() + "','" + FIRSTNAME.Text.ToUpper()+ "','" + M_NAME.Text.ToUpper() + "','" + LNAME.Text.ToUpper() + "','" + MOTHERNAME.Text.ToUpper() + "','" + LADDRESS.Text.ToUpper() + "','" + DISTRICT.Text.ToUpper() + "','" + STATE.Text.ToUpper() + "','" + PINCODE.Text.ToUpper() + "','" + P_ADDRESS.Text.ToUpper() + "','" +DISTRICT1.Text.ToUpper() + "','" + STATE1.Text.ToUpper() +"','" + PINCODE1.Text.ToUpper() + "','" + LANDLINE_NO.Text.ToUpper() + "','" + MOBILE_NO.Text.ToUpper() + "','" + EMAIL_ID.Text.ToUpper() + "','" + RadioButtonList1.SelectedItem.Text.ToUpper() + "','" + BasicDatePicker1.Text.ToUpper() + "')";cmd = new SqlCommand(sr, con);SqlParameter ImageName = new SqlParameter("@ImageName",SqlDbType.VarChar,50);ImageName.Value = strImageName.ToString();cmd.Parameters.Add(ImageName);SqlParameter UploadedImage = new SqlParameter("@Image",SqlDbType.Image,imagesize.Length);UploadedImage.Value = 图像大小;cmd.Parameters.Add(UploadedImage);con.Open();int 结果= cmd.ExecuteNonQuery();con.Close();如果(结果>0)msgBox2.alert("STUDENT记录创建成功");//cmd.Dispose();con.Close();}
insert.aspx

推荐答案

ConnectionStrings:ConnectionString %>"SelectCommand="SELECT [ImageName], [Image], [STUDENT_ID], [FNAME] FROM [STUDENT] ORDER BY [STUDENT_ID]"></asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1"onselectedindexchanged="GridView1_SelectedIndexChanged"><列><asp:BoundField DataField="ImageName" HeaderText="ImageName"SortExpression="ImageName"/><asp:TemplateField HeaderText="图片"><项目模板><asp:Image runat="服务器" id="image1" ImageUrl='<%# Eval("ID","Handler.ashx?ID={0}") + Eval("ID")%>'/></ItemTemplate></asp:TemplateField><asp:BoundField DataField="STUDENT_ID" HeaderText="STUDENT_ID" ReadOnly="True"SortExpression="STUDENT_ID"/><asp:BoundField DataField="FNAME" HeaderText="FNAME" SortExpression="FNAME"/></></asp:GridView>处理程序.asax<%@ WebHandler 语言="C#" ="Handler" %>使用系统;使用 System.Web;使用 System.Data.SqlClient;使用 System.Configuration;public class 处理程序:IHttpHandler {public void ProcessRequest(HttpContext context){SqlDataReader dr = null;SqlConnection con = null;SqlCommand cmd = null;试试{con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);cmd.CommandText = "select IMAGENAME,IMAGE from STUDENT where ID=@ID";cmd.CommandType = System.Data.CommandType.Text;cmd.Connection = con;SqlParameter ImageID = new SqlParameter("@ID",System.Data.SqlDbType.Int);ImageID.Value = context.Request.QueryString["ID"];cmd.Parameters.Add(ImageID);con.Open();博士 = cmd.ExecuteReader();博士读();context.Response.BinaryWrite((byte[])dr["图片"]);博士关闭();con.Close();}catch(例外){}}public bool IsReusable {get {return false;}}string strImageName = FileUpload1.FileName.ToString();if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName == ""){byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];HttpPostedFile uploadimage = FileUpload1.PostedFile;uploadimage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);String sr = "插入STUDENT(ImageName,Image,STUDENT_ID,FNAME,MNAME,LASTNAME,MOTHERNAME,LOCALADDRESS,DISTRICT,STATE,PINCODE,PERMENANTADDRESS,DISTRICT1,STATE1,PINCODE1,LANDLINENO,MOBILENO,EMAIL_ID,SEX,DATEOFBIRTH) 值(@ImageName,@Image,'"+STUDENT_ID.Text.ToUpper() + "','" + FIRSTNAME.Text.ToUpper()+ "','" + M_NAME.Text.ToUpper() + "','" + LNAME.Text.ToUpper() + "','" + MOTHERNAME.Text.ToUpper() + "','" + LADDRESS.Text.ToUpper() + "','" + DISTRICT.Text.ToUpper() + "','" + STATE.Text.ToUpper() + "','" + PINCODE.Text.ToUpper() + "','" + P_ADDRESS.Text.ToUpper() + "','" +DISTRICT1.Text.ToUpper() + "','" + STATE1.Text.ToUpper() +"','" + PINCODE1.Text.ToUpper() + "','" + LANDLINE_NO.Text.ToUpper() + "','" + MOBILE_NO.Text.ToUpper() + "','" + EMAIL_ID.Text.ToUpper() + "','" + RadioButtonList1.SelectedItem.Text.ToUpper() + "','" + BasicDatePicker1.Text.ToUpper() + "')";cmd = new SqlCommand(sr, con);SqlParameter ImageName = new SqlParameter("@ImageName",SqlDbType.VarChar,50);ImageName.Value = strImageName.ToString();cmd.Parameters.Add(ImageName);SqlParameter UploadedImage = new SqlParameter("@Image",SqlDbType.Image,imagesize.Length);UploadedImage.Value = 图像大小;cmd.Parameters.Add(UploadedImage);con.Open();int 结果= cmd.ExecuteNonQuery();con.Close();如果(结果>0)msgBox2.alert("STUDENT记录创建成功");//cmd.Dispose();con.Close();}insert.aspx

是不是没有从数据库中检索到,或者只是没有显示?这是两个不同的问题.尝试隔离问题.先判断是不是数据库的问题,然后是数据库代码,再是代码等等

此外,对于您的插入语句,NEVER 接受未经验证的用户输入并连接 SQL 语句.永远.您正在询问安全问题.了解 SQL 注入攻击.

本文地址:https://www.itbaoku.cn/post/1395167.html