Acess 存储与显示图片

在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>
posted on 2006-10-15 18:28  冰封王座(.net)博客  阅读(992)  评论(0编辑  收藏  举报