script抛出mysqlexception,并显示语法错误消息

kh212irz  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(281)

我想在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行'

ffvjumwh

ffvjumwh1#

去掉@time的空格,在“values”前加空格,就可以了

相关问题