验证并试图解决:Gridview无法直接绑定数据库中的图片(注意,是
以前看过一篇文章,说的是Gridview无法直接绑定数据库中的图片,要用HttpHandler...,原文里说我们都知道,在Gridview中不能直。。。,可惜我比较菜,当时确实不知道为什么不能绑定。今天练手,就拿这个来学习吧。 本文只是很浅的讨论一下这个绑定的技术,
以前看过一篇文章,说的是“Gridview无法直接绑定数据库中的图片,要用HttpHandler...”,原文里说“我们都知道,在Gridview中不能直。。。”,可惜我比较菜,当时确实不知道为什么不能绑定。今天练手,就拿这个来学习吧。
本文只是很浅的讨论一下这个绑定的技术,并不是赞同将图片保存在数据库里。
好吧,开始,先要做一个上传图片到数据库的页面,通过一步一步的写代码,我恍然大悟,确实是很明显的不能直接绑定嘛。
我实现上传图片的关键代码是这样的
<p><span> FileStream fs </span><span>=</span><span> File.Open(FileUpload1.PostedFile.FileName,FileMode.Open);<br><br> </span><span>byte</span><span>[] bt </span><span>=</span><span>new</span><span>byte</span><span>[fs.Length];<br><br> fs.Read(bt,</span><span>0</span><span>,(</span><span>int</span><span>)fs.Length);<br></span><span>//</span><span>other code</span><span><br></span><span> cmd.CommandText </span><span>=</span><span>"</span><span>insert into MyImage(image) values(@img)</span><span>"</span><span>;<br> SqlParameter sp </span><span>=</span><span>new</span><span> SqlParameter(</span><span>"</span><span>@img</span><span>"</span><span>,bt);<br></span><span>//</span><span>其他代码不上了</span></p>
显然的,图片文件被转换成了 byte[],然后存入数据库中 IMAGE类型的字段,
Gridview从数据库里读取的“图片”其实就是byte[],而我的理解就是,Gridview没有能力把byte[]转换成图片!
顺便说一下,在设计视图里测试的结果是这样的:
当时我还很诧异,以为绑定成功了。
但在浏览器里运行的时候,才发现真的不能绑定:
==========================================================
其实,光看别人的标题,还以为有多复杂,直到自己亲手写了一次,才发现其实不算难。
就是新建一个页面,该页面根据QueryString传递的参数去查询数据库里的IMAGE字段,然后用context.response.xx 输出,收工。
例子里在输出后用到了
Context.Response.End();
等下去查下资料,看这段代码到底有什么作用。
之前我做的毕业设计里面的“验证码功能”也用到了类似的处理办法,就是新建一个页面,负责产生图片格式的验证码,当时我使用context.response.write输出。
Write方法有三个重载,1是 char, 2是object,3是string。
而 Context.Response.BinaryWrite只有一种参数,是 byte[],如图:
正好和这里我使用的byte[]吻合,所以要用这个方法。
===========================================================
在gridview里,新建一个ImageField,绑定地址是
~/CreateIMG.aspx?id={0}
绑定的参数就是id。
这样就完成了所谓的“利用HttpHandler绑定”,主要是我以前用过这种方式,所以理解起来还是比较快的。
不过,对这个HttpHandler我还是真的不懂,还需要仔细的学习一下。
囧,要学习的东西真多啊,只有慢慢来咯。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.
