0

I want to save the system log in MySQL database and this is my code

EventRecord record;
while ((record = reader.ReadEvent()) != null)
{
    using (record)
    {
        Console.WriteLine("{0} {1}: {2} : {3}   {4}", record.TimeCreated, record.LevelDisplayName, record.FormatDescription(), record.Id, record.LogName);
        var time = record.TimeCreated;
        String Displayname = record.LevelDisplayName;
        String Description = record.FormatDescription();
        String log_name = record.LogName;
        int id = record.Id;

          string server;
          string database;
          string uid;
          string password;

        MySqlConnection conn;
        server = "localhost";
        database = "logfile";
        uid = "root";
        password = " ";

        string connetionString;
        connetionString = "SERVER=" + server + ";" + "DATABASE=" +database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";SSL Mode=none;";
        conn = new MySqlConnection(connetionString);
        string insertQuery = "insert into syslog (Time,Record_d,log_name,Display_Name,Description)values (@ time,@id,@log_name,@Displayname,@Description)";
        conn.Open();
        MySqlCommand cmd = new MySqlCommand(insertQuery, conn);
            cmd.Parameters.AddWithValue("@time", record.TimeCreated);
            cmd.Parameters.AddWithValue("@id", record.Id);
            cmd.Parameters.AddWithValue("@log_name", record.LogName);
            cmd.Parameters.AddWithValue("@Displayname", record.LevelDisplayName);
            cmd.Parameters.AddWithValue("@Description", record.FormatDescription());
            cmd.ExecuteNonQuery();
            conn.Close();

My script end with the following error:

MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's success status was true. The last boot's success status was true.')' at line 1'

David
  • 4,027
  • 10
  • 50
  • 102
Rathaan
  • 3
  • 3
  • 6
    Have you tried removing the space between @ and time in (@ time,@id,@log_name,@Displayname,@Description)? – Prachi Jul 19 '18 at 07:17
  • 1
    Possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](https://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) _`Time` is a reserved word._ – mjwills Jul 19 '18 at 07:18
  • use something other than @ time use @ timeCreated and do not forget to delete the space – Jiří Herník Jul 19 '18 at 08:24
  • Prachi and jiri Hernik i had solved the error by removing the space between @ time. thanks for you help. – Rathaan Jul 19 '18 at 13:34

1 Answers1

1

Remove space of @ time and add space before "values", that should do the trick

JohnMcClane
  • 128
  • 6