本文介绍了我想在网格视图中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我希望在网格视图中显示图片并添加数据我的代码是.... 使用系统; 使用 System.Collections.Generic; 使用 System.Data; 使用 System.Data.SqlClient; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls; 命名空间 UploadImages { public partial class WebForm1:System.Web.UI.Page { SqlConnection conn = new SqlConnection(); 受保护 void Page_Load( object sender,EventArgs e) { conn.ConnectionString = 数据源= DELL-PC ;初始目录= db;集成安全性=真; Load_GridData(); // 调用方法 } void Load_GridData() { conn.Open(); // 打开连接 SqlDataAdapter Sqa = new SqlDataAdapter( select * from ImageToTable, DataSet ds = new DataSet(); Sqa.Fill(ds); // 填充数据集 GridView1.DataSource = ds; // 提供数据to GridView GridView1.DataBind(); conn.Close(); } } } 我的Xml代码 <%@ 页面 语言 = C# AutoEventWireup = true CodeBehind = WebForm1.aspx.cs 继承 = UploadImages.WebForm1 %> < ! DOCTYPE html > < html xmlns = http://www.w3.org/1999/xhtml > < head runat = server > < title > < / title > < / head > < body > < form id = form1 runat = 服务器 > ; < div > < asp:GridView ID = GridView1 runat = 服务器 AutoGenerateColumns = 错误 > < 列 > < asp:BoundField DataField = name HeaderText = 名称 / > < asp:ImageField DataImageUrlField = myphoto HeaderText = images > < / asp:ImageField > < / Columns > < / asp:GridView > < / div > < / form > < / body > < / html > conn); Plz我一步一步地回答我的数据库是 myphoto image 已检查名称 nvarchar ( 50 )已检查 id nchar ( 10 )未选中 解决方案 在你的项目中添加一个新的aspx页面,如GetImg.aspx,它需要imageID作为参数。 添加新函数或直接在PageLoad SqlConnection con = new SqlConnection(your_连接); SqlCommand cmd = new SqlCommand(从表中选择Imatge,其中idImge ='+ paramIdImg +',con); con.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); if(reader.Read()) { Response.ContentType =image / jpg; Response.Clear(); Response.BufferOutput = true; SqlBinary sb = reader.GetSqlBinary(0); Response.BinaryWrite(sb.Value); Response.Flush(); } reader.Close(); con.Close(); gridview的列必须是这样的: < itemtemplate > > < asp:image id = Image1 runat = 服务器 backcolor = 透明 height = 75px imageurl = <% #Eval( YourDbFIeld, GetImg.aspx?paramImg = {0})%> width = 75px xmlns:asp = #unknown / > 此网站上有一篇文章: 在GridView中显示数据库中的图像 [ ^ ] i want show images in grid view and also add in data my code is....using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace UploadImages{ public partial class WebForm1 : System.Web.UI.Page { SqlConnection conn = new SqlConnection(); protected void Page_Load(object sender, EventArgs e) { conn.ConnectionString = "Data Source=DELL-PC;Initial Catalog=db;Integrated Security=True"; Load_GridData(); // call method below } void Load_GridData() { conn.Open(); // open the connection SqlDataAdapter Sqa = new SqlDataAdapter("select * from ImageToTable", DataSet ds = new DataSet(); Sqa.Fill(ds); // fill the dataset GridView1.DataSource = ds; // give data to GridView GridView1.DataBind(); conn.Close(); } }}My Xml Code<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UploadImages.WebForm1" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="name" HeaderText="name" /> <asp:ImageField DataImageUrlField="myphoto" HeaderText="images"> </asp:ImageField> </Columns> </asp:GridView> </div> </form></body></html>conn);Plz answer me step by step my db ismyphotoimageCheckednamenvarchar(50)Checkedidnchar(10)Unchecked 解决方案 Add a new aspx page in your project like GetImg.aspx, that requires the imageID as a param.Add a new function or directly in PageLoadSqlConnection con = new SqlConnection(your_connection);SqlCommand cmd = new SqlCommand("select Imatge from table where idImge ='" + paramIdImg + "'", con);con.Open();SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);if (reader.Read()){Response.ContentType = "image/jpg";Response.Clear();Response.BufferOutput = true;SqlBinary sb = reader.GetSqlBinary(0);Response.BinaryWrite(sb.Value);Response.Flush();}reader.Close();con.Close();The column of your gridview must be something like this:<itemtemplate>><asp:image id="Image1" runat="server" backcolor="Transparent" height="75px" imageurl="<%# Eval("YourDbFIeld", "GetImg.aspx?paramImg={0}") %>" width="75px" xmlns:asp="#unknown" />There's an article on this site:Displaying Images from a Database in a GridView[^] 这篇关于我想在网格视图中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 22:40
查看更多