c# – 语法错误附近:连接到mysql数据库时

c# – 语法错误附近:连接到mysql数据库时,第1张

概述我已经阅读了很多主题,但它们都没有实际工作,这就是为什么我要问一个新问题.好吧,所以我正在尝试将值插入我的MySQL数据库,但我收到了错误. MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corre

我已经阅读了很多主题,但它们都没有实际工作,这就是为什么我要问一个新问题.

好吧,所以我正在尝试将值插入我的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

该页面列出了关键字,但只保留了这些关键字的一个子集,由列表中的(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`,...
总结

以上是内存溢出为你收集整理的c# – 语法错误附近:连接到mysql数据库时全部内容,希望文章能够帮你解决c# – 语法错误附近:连接到mysql数据库时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1168720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存