脚本引发MySqlException并显示语法错误消息

时间:2018-07-19 07:14:53

标签: c# mysql logfiles

我想将系统日志保存在MySQL数据库中,这是我的代码

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();

我的脚本以以下错误结束:

  

MySql.Data.MySqlClient.MySqlException:'您的SQL错误   句法;检查与您的MySQL服务器版本相对应的手册   正确的语法以使用near的成功状态为true。最后   引导的成功状态为true。')'在第1行'

1 个答案:

答案 0 :(得分:1)

删除@时间的空间并在“值”之前添加空间,这应该可以解决问题