我已经阅读了很多主题,但它们都没有实际工作,这就是为什么我要问一个新问题.
好吧,所以我正在尝试将值插入我的MySQL数据库,但我收到了错误.
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 ‘Desc,Detectors,DetectorNos,
Question1,Question2,SpecialPrec,OfficerSign,Of’ at line 1′
我无法找到问题所在.我已经尝试了几个小时没有任何结果,甚至读了一百遍但仍然没有运气.所以我需要别人来看看.
public string detector = "";
public string questions = "";
public string question2 = "";
public string capOrCheif = "";
private void btn_send_Click(object sender,EventArgs e)
{
if(cBox_detectors_yes.Checked == true)
{
detector = "Yes";
}
if(cBox_yes1.Checked == true && cBox_yes3.Checked == true && cBox_yes4.Checked == true && cBox_yes5.Checked == true && cBox_yes6.Checked == true && cBox_yes7.Checked == true)
{
questions = "Yes";
}
if(cBox_yes2.Checked == true)
{
question2 = "Yes";
}
if(cBox_cheif.Checked == true)
{
capOrCheif = "Cheif Engineer";
}
else if(cBox_captain.Checked == true)
{
capOrCheif = "Captain";
}
else if(cBox_na2.Checked == true)
{
question2 = "N/A";
}
else if(cBox_detectors_na.Checked == true)
{
detector = "N/A";
}
string constring = "Server = **; Database = **; User Id = **; Password = ***; Sslmode = none;";
string Query = "INSERT INTO tbl_permit (Username,Ship,Date,TimeFrom,TimeTo,Location,Desc,Question1,OfficerName,OfficerPos,CheifSign,CheifName,CaptainSign,CaptainName,PrecAddedBy,PrecBox) values(@Username,@Ship,@Date,@TimeFrom,@TimeTo,@Location,@Desc,@Detectors,@DetectorNos,@Question1,@Question2,@SpecialPrec,@OfficerSign,@OfficerName,@OfficerPos,@CheifSign,@CheifName,@CaptainSign,@CaptainName,@PrecAddedBy,@PrecBox);";
MysqLConnection con = new MysqLConnection(constring);
MysqLCommand cmdDatabase = new MysqLCommand(Query,con);
cmdDatabase.Parameters.Add("@Username",MysqLDbType.VarChar,50).Value = login.username;
cmdDatabase.Parameters.Add("@Ship",50).Value = txtBox_ship.Text;
cmdDatabase.Parameters.Add("@Date",50).Value = txtBox_date.Text;
cmdDatabase.Parameters.Add("@TimeFrom",50).Value = txtBox_timeFrom.Text;
cmdDatabase.Parameters.Add("@TimeTo",50).Value = txtBox_timeTo.Text;
cmdDatabase.Parameters.Add("@Location",50).Value = txtBox_location;
cmdDatabase.Parameters.Add("@Desc",50).Value = txtBox_work_desc.Text;
cmdDatabase.Parameters.Add("@Detectors",50).Value = detector;
cmdDatabase.Parameters.Add("@DetectorNos",50).Value = txtBox_detector_desc.Text;
cmdDatabase.Parameters.Add("@Question1",50).Value = questions;
cmdDatabase.Parameters.Add("@Question2",50).Value = question2;
cmdDatabase.Parameters.Add("@SpecialPrec",50).Value = txtBox_precautions.Text;
cmdDatabase.Parameters.Add("@OfficerSign",50).Value = txtBox_officer_sign.Text;
cmdDatabase.Parameters.Add("@OfficerName",50).Value = txtBox_officer_name.Text;
cmdDatabase.Parameters.Add("@OfficerPos",50).Value = txtBox_officer_pos.Text;
cmdDatabase.Parameters.Add("@CheifSign",50).Value = txtBox_cheif_sign.Text;
cmdDatabase.Parameters.Add("@CheifName",50).Value = txtBox_cheif_name.Text;
cmdDatabase.Parameters.Add("@CaptainSign",50).Value = txtBox_captain_sign.Text;
cmdDatabase.Parameters.Add("@CaptainName",50).Value = txtBox_captain_name.Text;
cmdDatabase.Parameters.Add("@PrecAddedBy",50).Value = capOrCheif;
cmdDatabase.Parameters.Add("@PrecBox",50).Value = txtBox_restrictions.Text;
MysqLDataReader myReader;
if (cBox_read.Checked == true)
{
con.Open();
myReader = cmdDatabase.ExecuteReader();
while (myReader.Read())
{
}
MessageBox.Show("Hot Work Permit Form has been sent to the Cheif Engineer");
}
else
{
MessageBox.Show("You have to read it through and accept it!");
}
}
最佳答案
尽管有上述评论,DATE不是保留字.您可以在此处获取保留字列表:https://dev.mysql.com/doc/refman/8.0/en/keywords.html
原文链接:/mysql/432901.html该页面列出了关键字,但只保留了这些关键字的一个子集,由列表中的(R)注释表示.
错误消息告诉您哪个词导致解析器混淆:
…check the manual that corresponds to your MysqL server version for the right Syntax to use near ‘Desc,…
它对DESC这个词感到困惑. MysqL中的语法错误总是向您显示查询的一部分,从它混淆的那一刻开始.
在这种情况下,DESC是导致问题的保留字. DESC在我链接的关键字列表中有(R)注释.
您应该划定与保留字冲突的标识符:
string Query = "INSERT INTO tbl_permit (... Location,`Desc`,...