c# - 在Mysql中使用正确的语法错误

标签 c# asp.net mysql

我无法修复以下错误:

`"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 'When,Then) values( '79','WBT-CoE','gyj','yi','yi')' at line 1"` error.

代码如下:

 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);
        }
    }
    }

最佳答案

用 `` 包围 When 和 then,因为它们是保留名称。

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

关于c# - 在Mysql中使用正确的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17363735/

相关文章:

c# - Linq 的 except 方法不适用于字节值

c# - 使用 CaSTLe Windsor 在服务类的构造函数中使用参数化构造函数初始化类

ASP.NET 控件

c# - 跟踪用户点数(如 SO)

c# - 慢速 EF 查询按月/年分组数据

mysql - 如何在 Laravel 中对访问器进行排序?

php - 全文搜索方法不起作用

c# - 单元测试 WCF REST 服务

c# - 无法连接到 Redis 服务器;创建断开的多路复用器

asp.net - Automapper 有什么用?