Convert File To Byte-Array, Save In Access-DB, Rea

2019-03-01 04:44发布

问题:

Hello,

I am trying to

  1. Convert a file to a byte[]
  2. Write byte[] in a Access Database
  3. Read byte[] from DB
  4. Recreate the file from that


1.

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

2.

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();
    }

3.

        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());

4.

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

The thing is, newbytes is way smaller than bytes and the new file isn't accessable. In the Access DB cell are like chinese letters. I already tried to use other encodings. Nothing. Anyone can explain why it's so much smaller (23000 chars to 400 chars). Thanks.

UPDATE Added code for the other function

    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;
    }

回答1:

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

You're never assigning a value to the @file parameter.

Also, there is no need to convert to a string when reading the data, this should work instead:

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


回答2:

SOLUTION

    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));

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


标签: c# sql access