-1

I have some task. I must to implement a text editor with the ability to save / download files to / from the ms access database, but i don't know how it to do. There is my try(it doesn't work. only save files). I know, it's stupid, but..

    OleDbCommand cmd = new OleDbCommand();

    OleDbConnection cn = new OleDbConnection(
            @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\Database1.accdb;Persist Security Info=True");

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
            if(textBox1.Text != null)
            {
                FileInfo file = new FileInfo(textBox1.Text.ToString() + ".dat");
                using (BinaryWriter bw = new BinaryWriter(file.OpenWrite()))
                {
                    string text = textBox2.Text.ToString();
                    bw.Write(text);

                    try
                    {
                        cn.Open();
                        cmd.CommandText = "INSERT INTO info ( files.FileData ) values (@file)";
                        cmd.Parameters.Add("@file", file);
                        cmd.ExecuteNonQuery();
                        cn.Close();
                    }
                    catch (Exception ex)
                    {
                        cn.Close();
                        MessageBox.Show(ex.Message.ToString());
                    }

                    textBox1.Text = null;
                    textBox2.Text = null;
                }

            }
    }  

Is there any way to directly upload files to the database?

Michael.M
  • 13
  • 5
  • Could you be more clear as to what is going wrong, what your question is? – BradleyDotNET Jan 20 '15 at 23:57
  • I can't insert file into database. My code is wrong. I think this line is incorrectly cmd.Parameters.Add("@file", file); – Michael.M Jan 21 '15 at 00:16
  • So what are you trying to put in the database? All the text? binary? You can't just "put a file" in a database – BradleyDotNET Jan 21 '15 at 00:21
  • I trying to put in databes some information(text) in file. No matter in which. Why not? Access has a field of type "attachment". Working in MS Office I can download the file to the cell, why this can not be done by the program? – Michael.M Jan 21 '15 at 00:27
  • Because a "file" is just an abstraction. Its actually a place on the hard disk that has *another* chunk of space on the hard disk (called a file descriptor) that points at it. You can't just store that. Now, you can store *the path to the file* or *all the text in the file* or even *the binary representation of the file*. You cannot, however, store the file descriptor itself, as that data is only on the filesystem. Not sure which of those Office is doing. – BradleyDotNET Jan 21 '15 at 00:34
  • Your words have logic, but if you open MS Access and create new table, in constructor you can see type "attachment"(or or something similar). and if you check this type, than you can easely download file. – Michael.M Jan 21 '15 at 00:47
  • Judging by this documentation: https://support.office.com/en-US/Article/Attach-files-and-graphics-to-the-records-in-your-database-d40a09ad-a753-4a14-9161-7f15baad6dbd?ui=en-US& It looks like its taking the binary form, compressing it, and storing that. You would need to do something similar. Normal DBMS don't have "Attachment" types, so I'm not sure how to set that up with standard SQL. (and no, Access is nowhere near a normal DBMS). – BradleyDotNET Jan 21 '15 at 00:50
  • So, I must save my file in binary form, than compressing it... and after then I can download it? I agree, this task is strang. I would gladly have stored address of the file (or something similar), as you said, but the problem is specified MS Access database.( – Michael.M Jan 21 '15 at 00:58
  • Even in text form you can *read* it as binary. Honestly, I'm not sure how they are doing their insert. If it was available to me, I would either change the field to a string and just store the path, or to a VARBINARY and store it as binary. Granted, the second option may not be available in Access and you may not have the option of changing it anyways. Good luck! – BradleyDotNET Jan 21 '15 at 01:16

1 Answers1

1

If, as suggested in your comments to the question, you are looking to store the files in at Attachment field in the Access database then there is C# code for that in another question here:

Storing an image into an Attachment field in an Access database

Notes:

  1. Attaching the file(s) to a database record cannot be done with SQL. You need to use ACE DAO (as illustrated in the article cited above).

  2. Some file types are blocked from attachment fields. There is a list of those file types in the Microsoft Support article here.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418