我在我的程序中遇到 MySqlException 错误。这是一个简单的测试程序,我试图将带有时间戳的日志错误插入数据库中的表中。
// - - - - - - -程序 - - - - - - - - - - -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
string Log = "Sample Log Error";
string key = "11111";
string date;
string time;
int unixT;
TimeSpan unix_time;
// geting current date in yyyy-mm-dd format
date = DateTime.UtcNow.ToString("yyyy-MM-dd");
//getting current time in hh:mm:ss format
time = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
// Getting Unix Time
unix_time = (System.DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
//converting Unix time to seconds
unixT = Convert.ToInt32(unix_time.TotalSeconds);
MySqlConnection myConn = new MySqlConnection("Server=172.178.2.143;" + "Port=3306;" + "Database=DB;" + "Uid=user;" + "Pwd=pass;");
try
{
myConn.Open();
}
catch (MySqlException e)
{
Console.WriteLine(e);
}
MySqlCommand comm1 = new MySqlCommand("INSERT INTO Logger(date_stamp, time_stamp, log, unix_time, key) VALUES(@date_stamp, @time_stamp, @log, @unix_time, @key)", myConnection);
//inserting each element from GPS split message into its respective table and column in the database
comm1.Parameters.AddWithValue("date_stamp", date);
comm1.Parameters.AddWithValue("time_stamp", time);
comm1.Parameters.AddWithValue("log", Log);
comm1.Parameters.AddWithValue("unix_time", unixT);
comm1.Parameters.AddWithValue("key", key);
try
{
comm1.ExecuteNonQuery(); //executing the INSERT Query Above
}
catch (MySqlException e)
{
Console.WriteLine(e);
}
myConnection.Close();
// ------------结束程序------
我收到以下错误:
“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在 'key) VALUES('2013-09-13', '16:21:15 PM', '第 1 行的示例日志错误'、1379103676、'11111''"