0

我无法修复以下错误:

`“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 'When,Then) 值附近使用的正确语法('79','WBT-CoE','gyj',' yi','yi')' 在第 1 行"` 错误。

这是代码:

 protected void Button3_Click(object sender, EventArgs e){

        string MyconnectionString = "server=localhost;database=requirement_doc;Uid=t;Pwd=123;";
        MySqlConnection conn = new MySqlConnection(MyconnectionString);
        MySqlCommand cmd;

        DataTable dt1 = new DataTable();
        cmd = conn.CreateCommand();

        cmd.CommandText = "SELECT  Req_ID, Actor FROM UseCase where Req_ID='" + txtReqID.Text + "' AND Actor='" + DropDownList1.Text + "'";
                    MySqlDataAdapter da1 = new MySqlDataAdapter();
        da1.SelectCommand = cmd;
        da1.Fill(dt1);

        if (dt1.Rows.Count > 0)
        {

            Label1.Text = "Data already exist";

        }


        else
        {


            string sql = "INSERT INTO UseCase (Req_ID,Actor,Given,When,Then) values(  '" + txtReqID.Text + "','" + DropDownList1.Text + "','" + giventxt.Text + "','" + whentbl.Text + "','" + thentbl.Text + "')"; 

                          cmd.Connection = conn;
            cmd.CommandText = sql;
            conn.Open();



        }
        try
        {

            cmd.ExecuteNonQuery();
            Label1.Text = " Successfully saved";
        }

        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    }
4

2 回答 2

2

用 ` 将 When 和 then 括起来,因为它们是保留名称。

string sql = "INSERT INTO UseCase (Req_ID,Actor,Given,`When`,`Then`) values(  '" + txtReqID.Text + "','" + DropDownList1.Text + "','" + giventxt.Text + "','" + whentbl.Text + "','" + thentbl.Text + "')"; 
于 2013-06-28T11:34:10.277 回答
1

When并且Then是 MySQL 中的保留名称。因此,如果您将它们用作列名,则会出现该错误。

于 2013-06-28T11:33:05.780 回答