我在嘗試使用MySQL創建表時遇到錯誤。我無法弄清楚它是從哪裏來的。我認爲這可能是一個保留字「名稱」,但我改名CharName認爲這會有所幫助。任何人都有這方面的見解?創建新表時出現「SQL語法錯誤」
這裏是我的字符串:
CREATE TABLE xxx.xxx.xxx.xxx_10FEED988A06 (
`Date` datetime NOT NULL ,
`Guid` varchar(255) NULL ,
`CharName` varchar(255) NULL ,
`Server` varchar(255) NULL ,
`CpuId` varchar(255) NULL ,
`HardDriveId` varchar(255) NULL ,
`MacAddress` varchar(255) NULL ,
`IpAddress` varchar(255) NULL ,
`MD5` varchar(255) NULL ,
PRIMARY KEY (`Date`)
)
這是我的錯誤:
MySql.Data.MySqlClient.MySqlException (0x80004005): 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 '70.210.131.54_10FEED988A06 (`Date` datetime NOT NULL
,`Guid` varchar(255) NULL' at line 1
這裏是我的代碼:
public static void CreateNonUsersTable()
{
const string myConString = "Server=" + Server + ";" +
"PORT=" + Port + ";" +
"DATABASE=" + NonusersDataBase + ";" +
"Persist Security Info=No;" +
"Convert Zero Datetime=True;" +
"Encrypt=True;" +
"SslMode=Required;" +
"username=" + DbUser + ";" +
"password=" + DbPass + ";";
string createUser = @"CREATE TABLE " + Username + " (" +
"`Date` datetime NOT NULL ," +
"`Guid` varchar(255) NULL ," +
"`CharName` varchar(255) NULL ," +
"`Server` varchar(255) NULL ," +
"`CpuId` varchar(255) NULL ," +
"`HardDriveId` varchar(255) NULL ," +
"`MacAddress` varchar(255) NULL ," +
"`IpAddress` varchar(255) NULL ," +
"`MD5` varchar(255) NULL ," +
"PRIMARY KEY (`Date`))";
Logger.Write(myConString, Enums.LogType.Info);
Logger.Write(createUser, Enums.LogType.Info);
using (var conn = new MySqlConnection(myConString))
{
conn.Open();
using (var cmd = new MySqlCommand(createUser, conn))
{
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Logger.Write(ex.ToString(), Enums.LogType.Exception);
}
finally
{
cmd.Dispose();
}
}
conn.Close();
conn.Dispose();
}
}
有點不相關:如果您的'用戶名'變量包含用戶提供的數據,那麼您可能會面臨SQLI漏洞。 – LB2