gpt4 book ai didi

c# - 脚本抛出 MySqlException 并显示语法错误消息

转载 作者:行者123 更新时间:2023-11-29 10:08:36 25 4
gpt4 key购买 nike

我想将系统日志保存在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: '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'

最佳答案

删除 @time 的空格并在“values”之前添加空格,这样就可以了

关于c# - 脚本抛出 MySqlException 并显示语法错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51416527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com