惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 冰封王座(.net)博客

实现qq的自动登录 - 冰封王座(.net)博客 - 博客园 创建带签名的cab包的完整流程 在C#中使用 makecert 创建自签名的证书 对比 javascript url编码 有关 java 与 C#细节不同 - 冰封王座(.net)博客 mysql常用命令 使用 DotnetOpenMail发送带附件的邮件 - 冰封王座(.net)博客 - 博客园 今天你处理异常了么? 防止SQL注入攻击 在DataGrid中进行值映射 - 冰封王座(.net)博客 - 博客园 UML视图使用 想要做个开源 大家谁有创意啊? 程序员每天该做的事 Google SiteMap的作用及协议格式详解[转摘] 取得客户端MAC Java学习之路:不走弯路,就是捷径 开发人员必备网站 css 外层保持固定高度的情况下 能随内层高度自适应变化 新的学习计划(10/8--10/15)
Acess 存储与显示图片
冰封王座(.net)博客 · 2006-10-15 · via 博客园 - 冰封王座(.net)博客

在Acess 存储图片:
   1.在Access中存储图片的字段为"Ole 对象"
   2.将上传的文件存到 字节数组 中.
   3.然后将该数据存到数据库里就行了.

 1 private void UpLoadFile()
 2    {
 3        //get the file
 4        Stream uploadStream = FileUpload1.PostedFile.InputStream;
 5        int fileLengh = FileUpload1.PostedFile.ContentLength;
 6        byte[] filedata = new byte[fileLengh];
 7        uploadStream.Read(filedata, 0, fileLengh);
 8        string filename = FileUpload1.PostedFile.FileName;
 9        string filetype = FileUpload1.PostedFile.ContentType;
10
11        //
12        OleDbConnection conn = new OleDbConnection(CONSTRING);
13        OleDbCommand comm = new OleDbCommand();
14        comm.Connection = conn;
15        
16        //comm.CommandText = "insert into person (name,photo) values("+TName.Text.Trim()+","++")";
17        //
18        comm.CommandText = "insert into person (name,photo) values (@name,@photo)";
19        //
20        OleDbParameter name = new OleDbParameter("@name",OleDbType.VarChar,50);
21        name.Value = "aaa";
22        comm.Parameters.Add(name);
23        OleDbParameter photo = new OleDbParameter("@photo", OleDbType.Binary);
24        photo.Value = filedata;
25        comm.Parameters.Add(photo);
26        //
27        conn.Open();
28        comm.ExecuteReader();
29        conn.Close();
30
31    }



在Access中显示图片:
        在ASP.NET2.0中显示数据库中的图片,可以利用HttpHandler(.ashx)页面动态显示图片.
        然后在GridView等控件的自定义模版中安置一个Image控件,并设置Image控件的ImageUrl属性为类似 XXX.ashx?photoId=1 即可显示图片
        实例代码:

 1using System;
 2using System.Web;
 3using System.Data.OleDb;
 4using System.IO;
 5
 6public class Handler : IHttpHandler {
 7    
 8    public void ProcessRequest (HttpContext context) {
 9        context.Response.ContentType = "image/jpeg";
10        context.Response.Cache.SetCacheability(HttpCacheability.Public);
11        context.Response.BufferOutput = false;
12        int personId = -1;
13        Stream stream = null;
14        if (context.Request.QueryString["PersonID"!= null && context.Request.QueryString["PhotoID"!= "")
15        {
16        personId = Convert.ToInt32(context.Request.QueryString["PersonID"]);
17        stream = GetPhoto(personId);
18        }

19        const int buffersize = 1024 * 16;
20        byte[] buffer = new byte[buffersize];
21        int count = stream.Read(buffer, 0, buffersize);
22        while (count > 0)
23        {
24            context.Response.OutputStream.Write(buffer, 0, count);
25            count = stream.Read(buffer, 0, buffersize);
26         }

27    }

28    
29    public Stream GetPhoto(int personId)
30    {
31        OleDbConnection myConnection = new OleDbConnection();
32        myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\db1.mdb";
33        OleDbCommand myCommand = new OleDbCommand("SELECT Photo FROM Person WHERE Id=@Id",myConnection);
34        myCommand.Parameters.Add(new OleDbParameter("@Id", personId));
35        myConnection.Open();
36        object result = myCommand.ExecuteScalar();
37
38        try 
39        {
40            return new MemoryStream((byte[])result);
41        }

42        catch (ArgumentNullException e)
43        {
44            return null;
45        }

46        finally{
47            myConnection.Close();
48        }

49    }

50
51 
52    public bool IsReusable {
53        get {
54            return false;
55        }

56    }

57
58}

最后在控件的模板中可以这样帮定,实例代码:

1<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanged="GridView1_PageIndexChanged" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="1">
2            <Columns>
3                <asp:TemplateField HeaderText="Photo">
4                    <ItemTemplate>
5                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?PersonId=" + Eval("Id") %>' Width="200px" />
6                    </ItemTemplate>
7                </asp:TemplateField>
8            </Columns>
9        </asp:GridView>