Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

I can't insert text file as entry into access database using C#

$
0
0
 public static void AddNewEntry(string text, string fileName)
        {
            string path = Directory.GetCurrentDirectory();
            path = Path.Combine(path, fileName);

         
            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.Write(text);
            }

            FileInfo fi = new FileInfo(path);

            if(fi.Exists)
            {
                byte[] bData = null;
                // Read file data into buffer
                using (FileStream fs = fi.OpenRead())
                {
                    bData = new byte[fi.Length];                   
                    int nReadLength = fs.Read(bData, 0, (int)(fi.Length));
                }

                string strQuery = String.Format("INSERT INTO Files (content) VALUES (@filedata)");

                var command = new oledb.OleDbCommand(strQuery, connection);             

                command.Parameters.AddWithValue("@filedata", bData);

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();               
            }

            File.Delete(path);        
        }

I have a table named 'Files'. It has two atributes file_id(primary key, counter) and content(has type 'entry', I keep txt files in this field). As you can see I try to convert text file to binary and then I want to insert this binary file to the field 'content' into access database.

Please, help. Sincerely, Alex.

 

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>