现在的位置: 首页 > 综合 > 正文

Access 2007 数据库中存取和读取二进制图片

2011年12月11日 ⁄ 综合 ⁄ 共 3610字 ⁄ 字号 评论关闭

首先是在Access中建立一个表,这个表中用了存储图片文件的字段的类型设置为 OLE 对象,

 然后就是使用一个 FileUpLoad 上传一张图片,再读取上传的图片的字节流,并且将其储存在Access数据库中,

最后再使用一个泛型程序处理一下,读取出图片并且使用其得到的数据流来初始化成一张图片,

即将数据流重新写回图片,再在 Image 控件上显现出来

上传到Access中的代码大致如下:

            if (!FileUpload1.HasFile)

            {

                Label1.Text = "请先选择要上传的图片文件";

                return;

            }

            string[] allowType = new string[4];

            allowType[0] = ".jpg";

            allowType[1] = ".jpeg";

            allowType[2] = ".gif";

            allowType[3] = ".png";

            bool allow = false;

            foreach (string str in allowType)

            {

                if (str == System.IO.Path.GetExtension(FileUpload1.FileName).ToLower())

                {

                    allow = true;

                    break;

                }

            }

            if (allow == false)

            {

                Label1.Text = "你上传的图片格式不正确,请选择 .Jpg,.Jpeg,.Gif,.Png 格式的图片";

                return;

            }

            string conStr = "Provider=Microsoft.Ace.OleDb.12.0;";

            conStr += @"Data Source=E:\数据库\XiaoZhen.accdb;";

            string accessStr = "Insert Into 相册(相片名,大小,上传日期,相片) " +

                                       "Values(@Name,@Length,@Date,@Photo)";

            string name = FileUpload1.PostedFile.FileName.ToString();

            string length = FileUpload1.PostedFile.ContentLength.ToString();

            string date = DateTime.Now.ToShortDateString();

            try

            {

                using (OleDbConnection oleDbCon = new OleDbConnection(conStr))

                {

                    oleDbCon.Open();

                    using (OleDbCommand oleDbCom = oleDbCon.CreateCommand())

                    {

                        oleDbCom.CommandType = CommandType.Text;

                        oleDbCom.CommandText = accessStr;

                        oleDbCom.Parameters.AddWithValue("@Name", name);

                        oleDbCom.Parameters.AddWithValue("@Length", length);

                        oleDbCom.Parameters.AddWithValue("@Date", date);

                        oleDbCom.Parameters.Add("@Photo", OleDbType.LongVarBinary).Value = FileUpload1.FileBytes;

                        oleDbCom.ExecuteNonQuery();

                        Label1.Text = "恭喜你,图片上传成功";

                    }

                }

            }

            catch (Exception E)

            {

                Label1.Text = E.Message.ToString();

            } 

 

 

泛型程序代码则是取出图片并且显现,代码大致如下:

            string conStr = "Provider=Microsoft.Ace.OleDb.12.0;";

            conStr += @"Data Source=E:\数据库\XiaoZhen.accdb;";

            string photoName = context.Request.QueryString["PhotoName"].ToString();

            string accessAtr = "Select 相片 From 相册 Where 相片名='" + photoName + "'";

            try

            {

                using (OleDbConnection oleDbCon = new OleDbConnection(conStr))

                {

                    oleDbCon.Open();

                    using (OleDbCommand oleDbCom = oleDbCon.CreateCommand())

                    {

                        oleDbCom.CommandType = CommandType.Text;

                        oleDbCom.CommandText = accessAtr;

                        using (OleDbDataReader oleDbDR = oleDbCom.ExecuteReader())

                        {

                            if (oleDbDR.Read())

                            {

                                context.Response.ContentType = "Image/JPEG";

                                context.Response.Clear();

                                context.Response.BufferOutput = true;

                                using (MemoryStream ms = new MemoryStream((byte[])oleDbDR[0]))

                                {

                                    using (Bitmap bigPhoto = new Bitmap(ms))

                                    {

                                        using (Bitmap smallPhoto = new Bitmap(bigPhoto, Convert.ToInt32(bigPhoto.Width * 0.5),

                                                                                                                Convert.ToInt32(bigPhoto.Height * 0.5)))

                                        {

                                            smallPhoto.Save(context.Response.OutputStream, ImageFormat.Jpeg);

                                            smallPhoto.Save(HttpContext.Current.Server.MapPath("~/Photo/" + photoName));

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

            catch

            { 

            

            } 

 

顺便提一下,本人在使用 Visual Studio 2008 中的 AccessDataSource 时发现无法访问 Access 2007 的数据库.accdb格式的,

请高手指定,是否可以进行访问?或者是有其他地方需要注意的。。。

 

 

 

抱歉!评论已关闭.