c# - Convert File To Byte-Array, Save In Access-DB, Read From DB And Create File -


hello,

trying

  1. convert file byte[]
  2. write byte[] in access database
  3. read byte[] db
  4. recreate file 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 way smaller bytes , new file isn't accessable. in access db cell chinese letters. tried use other encodings. nothing. can explain why it's smaller (23000 chars 400 chars). thanks.

update added code other function

    public static datatable getfilebytearraybyid(int aid)     {         conn.open();          dbcommand = new oledbcommand("select datei belege (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;     } 

dbcommand.parameters.add("@id", oledbtype.integer).value = aid; dbcommand.parameters.add("@id", oledbtype.varbinary).value = afile; 

you're never assigning value @file parameter.

also, there no need convert string when reading data, should work instead:

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

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -