加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-12-11 23:50:17 所属栏目:MySql教程 来源:网络整理
导读:我已经阅读了很多主题,但它们都没有实际工作,这就是为什么我要问一个新问题. 好吧,所以我正在尝试将值插入我的MySQL数据库,但我收到了错误. MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that correspon

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读