1

I am working on a c# project with a MySQL database. I have a log file and I want to store this file in the database.

I want to save the actual file into the database but what I've seen is you have to encode it to a string or write the bytes to a string and put that in the database, is that how it works to store the file, I thought you could just give the command the file path and it stores it in the database without me requiring to do all the encoding in the software.

If this makes any difference, it will need to be retrieved again from PHP, but I'm guessing that this shouldn't matter.

dove
  • 20,469
  • 14
  • 82
  • 108
Boardy
  • 35,417
  • 104
  • 256
  • 447

5 Answers5

1

Yes you have to encode it to string or bytes to store the file. Another way would be store the log file location, and instead save the log file on the disk some where.

qelper
  • 91
  • 1
1

This may sound a little ludicrous but you should look into the LOAD_FILE function

Here is the example for the MySQL Documentation

mysql> UPDATE t
        SET blob_col=LOAD_FILE('/tmp/picture')
        WHERE id=1;

As long as the log file is small enough to fit into a BLOB, then that's your only shot at it

If the log file is too big, just archive the log file somewhere and save the file location instead

RolandoMySQLDBA
  • 43,883
  • 16
  • 91
  • 132
0

Your program is going to have to parse the log file and turn it into an SQL query.

Richard Hum
  • 651
  • 2
  • 6
  • 14
0

If you want to store the contents of the file in the database, then yes, you either encode it or store the bytes in the table column. There's no way around that.

If you want to simply store the path to the file, then all you need to do is pass in the path as parameter to your MySQLCommand but the moment you move the file around in the filesystem, your record will be invalid, obviously.

Link to a somewhat related question regarding which approach is best: Storing the content of the file in the database or simply the path in the file system.

Community
  • 1
  • 1
Icarus
  • 63,293
  • 14
  • 100
  • 115
0

Why not use Log4Net? It has the ability to log to databases automatically. Take a look at https://logging.apache.org/log4net/release/config-examples.html for details on how to connect a database to Log4Net

orignal decayed link: http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.html

Robert H
  • 11,520
  • 18
  • 68
  • 110
  • Your link has decayed, unfortunately. – JohnP Jan 26 '17 at 16:12
  • @JohnP Thanks - I added a link to config examples, unfortunately MySQL is not directly linked there, but the examples provide enough information to be able to figure it out – Robert H Jan 26 '17 at 16:55