将文件转换为字节数组,保存在Access-DB中,从数据库读取并创建文件

时间:2017-04-07 09:27:10

标签: c# sql access

您好,

我想

  1. 将文件转换为字节[]
  2. 在Access数据库中写入byte []
  3. 从DB
  4. 读取byte []
  5. 重新创建文件


    1。

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.0"
        defaultConfig {
            applicationId "com.aitrich.android.modern.photo"
            minSdkVersion 14
            targetSdkVersion 25
            multiDexEnabled true
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    
    apply plugin: 'com.neenbedankt.android-apt'
    apply plugin: 'org.greenrobot.greendao'
    
    ext {
        supportLibraryVersion = '25.0.1'
        playServicesVersion = '9.0.2'
        retrofitVersion = '2.1.0'
    }
    
    greendao {
        schemaVersion 1
    }
    
    apt {
        arguments {
            stagGeneratedPackageName "com.aitrich.android.modern.stag.generated"
            stagDebug true
        }
    }
    
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        testCompile 'junit:junit:4.12'
    
        compile "com.android.support:design:$supportLibraryVersion"
        compile "com.android.support:support-v13:$supportLibraryVersion"
        compile "com.android.support:cardview-v7:$supportLibraryVersion"
        compile 'org.greenrobot:eventbus:3.0.0'
        compile 'org.greenrobot:greendao:3.2.0'
        compile 'com.jakewharton:butterknife:8.4.0'
        compile 'com.intuit.sdp:sdp-android:1.0.3'
        compile 'me.zhanghai.android.materialprogressbar:library:1.3.0'
        compile 'com.mobsandgeeks:android-saripaar:2.0.3'
        compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
        compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
        compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
        compile 'com.squareup.picasso:picasso:2.5.2'
        compile 'com.vimeo.stag:stag-library:2.0.2'
        compile 'com.github.hotchemi:permissionsdispatcher:2.2.0'
        compile 'com.tapadoo.android:alerter:1.0.6'
        compile 'ch.halcyon:squareprogressbar:1.6.0'
        compile 'com.yalantis:ucrop:2.2.0'
        compile 'com.yalantis:ucrop:2.2.0-native'
        compile 'com.evernote:android-job:1.1.8'
        compile('com.github.silvestrpredko:dot-progress-bar:1.1') {
            exclude group: 'com.android.support', module: 'appcompat-v7'
        }
        compile('com.github.StevenDXC:DxLoadingButton:1.5') {
            exclude group: 'com.android.support', module: 'appcompat-v7'
        }
        compile('com.redmadrobot:chronos:1.0.7') {
            exclude module: 'eventbus:2.4.0'
        }
        compile ('co.infinum:materialdatetimepicker-support:3.1.3') {
            exclude group: 'com.android.support', module: 'design'
            exclude group: 'com.android.support', module: 'support-v4'
            exclude group: 'com.android.support', module: 'support-v13'
        }
        compile 'com.android.support:multidex:1.0.1'
    
    
        apt 'com.jakewharton:butterknife-compiler:8.4.0'
        apt 'com.vimeo.stag:stag-library-compiler:2.0.2'
        apt 'com.github.hotchemi:permissionsdispatcher-processor:2.2.0'
    }
    

    2

    byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\user\Docs\1.pdf");
    

    3。

    AccessConnector.WriteByteArrayToID(122, bytes);
    
        public static void WriteByteArrayToID(int aID, byte[] aFile)
        {
            conn.Open();
            dbCommand = new OleDbCommand("UPDATE Belege SET Datei = @file WHERE(ID = @ID)", conn);
    
            dbCommand.Parameters.Add("@ID", OleDbType.Integer).Value = aID;
            dbCommand.Parameters.Add("@file", OleDbType.VarBinary).Value = aFile.ToString();
    
            dbDataAdapter = new OleDbDataAdapter(dbCommand);
            dbCommand.ExecuteNonQuery();
    
            conn.Close();
        }
    

    4。

            DataTable table = AccessConnector.GetFileByteArrayByID(122);
            DataRow row = table.Rows[0];
    
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
    
            byte[] newbytes = enc.GetBytes(row.ItemArray[0].ToString());
    

    问题是,newbytes比字节小,新文件不可访问。 Access DB单元格就像中文字母。我已经尝试过使用其他编码。没有。任何人都可以解释为什么它这么小(23000个字符到400个字符)。谢谢。

    更新为其他功能添加了代码

                System.IO.File.WriteAllBytes(@"C:\Users\user\Documents\Docs\a.pdf", newbytes);
    

2 个答案:

答案 0 :(得分:0)

dbCommand.Parameters.Add("@ID", OleDbType.Integer).Value = aID;
dbCommand.Parameters.Add("@ID", OleDbType.VarBinary).Value = aFile;

您永远不会为@file参数分配值。

此外,在读取数据时无需转换为字符串,这应该可以改为:

byte[] newbytes = (byte[])table.Rows[0][0];

答案 1 :(得分:0)

解决方案

    public static void WriteByteArrayToID(int aID, byte[] aFile)
    {
        conn.Open();

        dbCommand = new OleDbCommand("UPDATE Belege SET Datei = @file WHERE(ID = @ID)", conn);

        string tmp = Convert.ToBase64String(aFile);

        dbCommand.Parameters.Add("@file", OleDbType.VarChar).Value = tmp;
        dbCommand.Parameters.Add("@ID", OleDbType.Integer).Value = aID;

        dbDataAdapter = new OleDbDataAdapter(dbCommand);
        dbCommand.ExecuteNonQuery();

        conn.Close();
    }

    public static DataTable GetFileByteArrayByID(int aID)
    {
        conn.Open();

        dbCommand = new OleDbCommand("SELECT Datei FROM Belege WHERE (ID = @ID)", conn);

        dbCommand.Parameters.Add("@ID", OleDbType.Integer).Value = aID;
        dbDataAdapter = new OleDbDataAdapter(dbCommand);
        DataTable resultDataTable = new DataTable();
        dbDataAdapter.Fill(resultDataTable);

        conn.Close();
        return resultDataTable;
    } 

        //-----------------------------------------------------------------------------------------------------------------

        byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\user\Music\1.pdf");

        AccessConnector.WriteByteArrayToID(122, bytes);

        DataTable table = AccessConnector.GetFileByteArrayByID(122);

        string file = table.Rows[0][0] as string;

        System.IO.File.WriteAllBytes(@"C:\Users\user\Documents\Dokumente\a.pdf", Convert.FromBase64String(file));

        //-----------------------------------------------------------------------------------------------------------------