1

I’m trying to upload docx document to Access database.

            OleDbConnection conn = null;
            OleDbDataReader reader = null;

            try
            {
                conn = new OleDbConnection(
                    "Provider=Microsoft.ACE.OLEDB.12.0; " +
                    "Data Source=" + @"C:\AudSystem\Auditoru_sertifikacija2007.accdb");
                conn.Open();


                OleDbCommand cmd = new OleDbCommand();

                cmd.CommandText = "insert into questions (Category, Question, Answers, CorrectAnswers, Comment, Reference, QuestionNumber, Changed, IsNew) values (@p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";

                byte[] file = File.ReadAllBytes(@"C:\AudSystem\rez.docx");

                cmd.Parameters.AddWithValue("@p2", 2);
                cmd.Parameters.AddWithValue("@p3", file);
                cmd.Parameters.AddWithValue("@p4", file);
                cmd.Parameters.AddWithValue("@p5", "asd");
                cmd.Parameters.AddWithValue("@p6", "fgh");
                cmd.Parameters.AddWithValue("@p7", "zxc");
                cmd.Parameters.AddWithValue("@p8", 1);
                cmd.Parameters.AddWithValue("@p9", true);
                cmd.Parameters.AddWithValue("@p10", true);

                cmd.Connection = conn;
                cmd.ExecuteNonQuery();


            }
            finally
            {
                if (reader != null) reader.Close();
                if (conn != null) conn.Close();

            }

All is fine, but when I open database table in design view, I see that in row is stored “Long binary data”, instead of “Microsoft word document”. If I save file to database through the Access form, file is stored as “Microsoft word document” and I can edit it using access form. So, how can I store programmaticly docx file to access OLE field as “Microsoft word document”?

In picture, I've saved same file in "Question" field by Access Form, but in "Answers" field by C# code.

enter image description here

Thanks.

user2783755
  • 578
  • 2
  • 10
  • 26
  • 1
    possible duplicate of [How to insert an image into an Access OLE field via C#](http://stackoverflow.com/questions/21497937/how-to-insert-an-image-into-an-access-ole-field-via-c-sharp). (That question deals with image files, but the same method would work for other OLE-embedded files like Word documents.) – Gord Thompson Apr 28 '14 at 12:14

0 Answers0