Acess 存储与显示图片

在Acess 存储图片:
   1.在Access中存储图片的字段为"Ole 对象"
   2.将上传的文件存到 字节数组 中.
   3.然后将该数据存到数据库里就行了.
 1 None.gif   private   void  UpLoadFile()
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        //get the file
 4InBlock.gif        Stream uploadStream = FileUpload1.PostedFile.InputStream;
 5InBlock.gif        int fileLengh = FileUpload1.PostedFile.ContentLength;
 6InBlock.gif        byte[] filedata = new byte[fileLengh];
 7InBlock.gif        uploadStream.Read(filedata, 0, fileLengh);
 8InBlock.gif        string filename = FileUpload1.PostedFile.FileName;
 9InBlock.gif        string filetype = FileUpload1.PostedFile.ContentType;
10InBlock.gif
11InBlock.gif        //
12InBlock.gif        OleDbConnection conn = new OleDbConnection(CONSTRING);
13InBlock.gif        OleDbCommand comm = new OleDbCommand();
14InBlock.gif        comm.Connection = conn;
15InBlock.gif        
16InBlock.gif        //comm.CommandText = "insert into person (name,photo) values("+TName.Text.Trim()+","++")";
17InBlock.gif        //
18InBlock.gif        comm.CommandText = "insert into person (name,photo) values (@name,@photo)";
19InBlock.gif        //
20InBlock.gif        OleDbParameter name = new OleDbParameter("@name",OleDbType.VarChar,50);
21InBlock.gif        name.Value = "aaa";
22InBlock.gif        comm.Parameters.Add(name);
23InBlock.gif        OleDbParameter photo = new OleDbParameter("@photo", OleDbType.Binary);
24InBlock.gif        photo.Value = filedata;
25InBlock.gif        comm.Parameters.Add(photo);
26InBlock.gif        //
27InBlock.gif        conn.Open();
28InBlock.gif        comm.ExecuteReader();
29InBlock.gif        conn.Close();
30InBlock.gif
31ExpandedBlockEnd.gif    }


在Access中显示图片:
        在ASP.NET2.0中显示数据库中的图片,可以利用HttpHandler(.ashx)页面动态显示图片.
        然后在GridView等控件的自定义模版中安置一个Image控件,并设置Image控件的ImageUrl属性为类似 XXX.ashx?photoId=1 即可显示图片
        实例代码:
 1 None.gif using  System;
 2 None.gif using  System.Web;
 3 None.gif using  System.Data.OleDb;
 4 None.gif using  System.IO;
 5 None.gif
 6 ExpandedBlockStart.gifContractedBlock.gif public   class  Handler : IHttpHandler  dot.gif {
 7InBlock.gif    
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    public void ProcessRequest (HttpContext context) dot.gif{
 9InBlock.gif        context.Response.ContentType = "image/jpeg";
10InBlock.gif        context.Response.Cache.SetCacheability(HttpCacheability.Public);
11InBlock.gif        context.Response.BufferOutput = false;
12InBlock.gif        int personId = -1;
13InBlock.gif        Stream stream = null;
14InBlock.gif        if (context.Request.QueryString["PersonID"!= null && context.Request.QueryString["PhotoID"!= "")
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif        personId = Convert.ToInt32(context.Request.QueryString["PersonID"]);
17InBlock.gif        stream = GetPhoto(personId);
18ExpandedSubBlockEnd.gif        }

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

27ExpandedSubBlockEnd.gif    }

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

42InBlock.gif        catch (ArgumentNullException e)
43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
44InBlock.gif            return null;
45ExpandedSubBlockEnd.gif        }

46ExpandedSubBlockStart.gifContractedSubBlock.gif        finallydot.gif{
47InBlock.gif            myConnection.Close();
48ExpandedSubBlockEnd.gif        }

49ExpandedSubBlockEnd.gif    }

50InBlock.gif
51InBlock.gif 
52ExpandedSubBlockStart.gifContractedSubBlock.gif    public bool IsReusable dot.gif{
53ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gif{
54InBlock.gif            return false;
55ExpandedSubBlockEnd.gif        }

56ExpandedSubBlockEnd.gif    }

57InBlock.gif
58ExpandedBlockEnd.gif}
最后在控件的模板中可以这样帮定,实例代码:
1 None.gif < asp:GridView ID = " GridView1 "  runat = " server "  AllowPaging = " True "  OnPageIndexChanged = " GridView1_PageIndexChanged "  OnPageIndexChanging = " GridView1_PageIndexChanging "  PageSize = " 1 " >
2 None.gif             < Columns >
3 None.gif                 < asp:TemplateField HeaderText = " Photo " >
4 None.gif                     < ItemTemplate >
5 None.gif                         < asp:Image ID = " Image1 "  runat = " server "  ImageUrl = ' <%# "Handler.ashx?PersonId=" + Eval("Id") %> '  Width = " 200px "   />
6 None.gif                     </ ItemTemplate >
7 None.gif                 </ asp:TemplateField >
8 None.gif             </ Columns >
9 None.gif         </ asp:GridView >

转载于:https://www.cnblogs.com/zhifengwu1211/archive/2006/10/15/529608.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值