Hello,
I am trying to
- Convert a file to a byte[]
- Write byte[] in a Access Database
- Read byte[] from DB
- 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;
}