• 大小: 41KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: access2007  ole对象  图片  c#  

资源简介

手工添加的access ole对象批量导出问题 困扰了我很久 现在已经解决了 希望能够帮助到大家! 附有用链接 http://dotnet.aspx.cc/file/Removing-OLE-Header-from-images-stored-in-MS-Access-DB-as-OLE-Object.aspx

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {

        }

        //以下是从手工添加了ole对象的access2007数据库中导出二进制图片到本地的方法 具体使用情况请根据实际进行修改
        //注意 手工添加ole对象与程序写入ole对象相比 会多出来一个文件头 所以在从数据库取出二进制图片后 需要进行去
        //文件头的步骤
        //该问题困扰了我很久 故上传 希望能帮助到大家
        //如果你有足够的能耐 请联系mail@misxiu.com  新创业公司 期待您的加盟!一起书写商业传奇!
        //2011年7月10日凌晨于杭州
        private void button1_Click(object sender EventArgs e)
        {

            string conStr = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\123.accdb“;
            string accessAtr = “Select 编号回单扫描图片 From 调度单 where 回单扫描图片 is not null “;


            OleDbConnection oleDbCon = new OleDbConnection(conStr);

            oleDbCon.Open();

            OleDbCommand oleDbCom = oleDbCon.CreateCommand();

            //oleDbCom.CommandType = CommandType.Text;

            oleDbCom.CommandText = accessAtr;

            OleDbDataReader oleDbDR = oleDbCom.ExecuteReader();

            try
            {
                while (oleDbDR.Read())
                { 
                    byte[] bfile = (byte[]) oleDbDR.GetValue(1);
                    bfile = GetImageBytesFromOLEField(bfile);//如果是手工添加的ole对象 必须执行此步骤
                   
                   using (MemoryStream ms = new MemoryStream(bfile))
                    {
                        try
                        {
                            using (Image bigPhoto = Image.FromStream(ms))
                            {
                                bigPhoto.Save(“E://photo/“ + oleDbDR.GetValue(0) + “.jpg“);
                            }
                        }

                        catch
                        {

                        }

                    }

                }
            }
            finally
            {
                oleDbDR.Close();
                oleDbCon.Close();
            }
        }


        //手工添加ole对象 去文件头方法
        private byte[] GetImageBytesFromOLEField(byte[] oleFieldBytes)
        {

            
            const string BITMAP_ID_BLOCK = “BM“;
            const string JPG_ID_BLOCK = “\u00FF\u00D8\u00FF“;
            const string PNG_ID_BLOCK = “\u0089PNG\r\n\u001a\n“;
            const string GIF_ID_BLOCK = “GIF8“;
            const string TIFF_ID_BLOCK = “II*\u0000“;


            byte[] imageBytes;


            // Get a UTF7 Encoded string version
            Encoding u8 = Encoding.UTF7;
            string strTemp = u8.GetString(oleFieldBytes);


            // Get the first 300 characters from the string
 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10240  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

     文件      22016  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb

     文件      11600  2011-07-09 23:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe

     文件       4782  2011-07-10 04:06  WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs

     文件       2037  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs

     文件       5817  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx

     文件       4440  2011-07-09 23:45  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6256  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        296  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        758  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\GenerateResource.write.1.tlog

     文件        907  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt

     文件      10240  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.exe

     文件        180  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.Form1.resources

     文件      22016  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.pdb

     文件        180  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.Properties.Resources.resources

     文件        505  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Program.cs

     文件       1380  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Properties\AssemblyInfo.cs

     文件       2896  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Resources.Designer.cs

     文件       5612  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Resources.resx

     文件       1107  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Settings.Designer.cs

     文件        249  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Settings.settings

     文件       3707  2011-07-09 19:52  WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1.csproj

     文件        914  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1.sln

    ..A..H.     18432  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1.suo

     目录          0  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\TempPE

     目录          0  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug

     目录          0  2011-07-09 23:55  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug

     目录          0  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86

     目录          0  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\bin

     目录          0  2011-07-09 19:50  WindowsFormsApplication1\WindowsFormsApplication1\obj

............此处省略6个文件信息

评论

共有 条评论