【问题标题】:System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'values'.'System.Data.SqlClient.SqlException:'关键字'值'附近的语法不正确。'
【发布时间】:2021-07-24 08:04:26
【问题描述】:
namespace login_page
{
    public partial class itemselect : Form
    {
        public itemselect()
        {
            InitializeComponent();
        }

        private void product_Click(object sender, EventArgs e)
        {

        }

        private void Addproduct_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-QI8RJIB;Initial Catalog=itemselect;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand(" insert into itemselect([Product ID],[Product Name],[Product Quantity],[Product Price] values ('" +pid.Text+ "','" +pn.Text+ "','" +pq.Text+ "','" +pp.Text+ "')", con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            this.Close();
            MessageBox.Show("item added successfully");

        }

例外:

System.Data.SqlClient.SqlException: '关键字附近的语法不正确 '价值观'。'

【问题讨论】:

  • 您在值之前缺少一个结束 ),它将包装 SQL 查询中的列名。
  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables

标签: c# asp.net sql-server webforms ado.net


【解决方案1】:

你在values这个词之前缺少了一个结束语。

  • 您还应该使用using 语句来确保连接 执行后将关闭
  • 您还应该使用Parameters.AddWithValue() 方法来避免 SQL INJECTION

【讨论】:

【解决方案2】:

您的语法不正确,请确保您已关闭所有打开的括号。

另外,请始终使用SqlParameters 来防止Sql injection 攻击并提高代码可读性

示例代码

private void Addproduct_Click(object sender, EventArgs e)
{
    string query = "INSERT INTO itemselect ([Product ID],[Product Name],[Product Quantity],[Product Price]) VALUES (@ProductID, @ProductName, @ProductQuantity, @ProductPrice)";
    using (var con = new SqlConnection(@"Data Source=DESKTOP-QI8RJIB;Initial Catalog=itemselect;Integrated Security=True"))
    using (var cmd = new SqlCommand(query, con))
    {
        // Not sure about ProductID type. Could be SqlDbType.UniqueIdentifier or SqlDbType.Int / BigInt
        cmd.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.UniqueIdentifier)).Value = pid.Text;
        cmd.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar)).Value = pp.Text;
        cmd.Parameters.Add(new SqlParameter("@ProductQuantity", SqlDbType.Int)).Value = pq.Text;
        cmd.Parameters.Add(new SqlParameter("@ProductPrice", SqlDbType.Decimal)).Value = pp.Text;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            // Records Inserted Successfully
        }
        catch (SqlException err)
        {
            // Error occured. Handle error
        }
    }
}

P.S:在命名表、列和变量时请遵循命名约定

【讨论】:

  • Addwithvalue 显然有问题。而且我不希望数量或价格具有文本类型。但是是的,一定要使用参数
  • @HansKesting:addwithvalue 的隐式转换。忽略了它。已经有一段时间没有使用 websorms 了。谢谢指出
  • 好答案;缺少一些usings,老实说:我强烈主张在所有“运行自己的 SQL”场景中使用 Dapper:con.Execute(sql, new { ProductId = pid.Text, ... }); - 更不用说出错了
  • @MarcGravell:我们在 EF Core 中几乎广泛使用 Dapper。考虑不为 OP 添加额外的层。更新代码。
猜你喜欢
  • 2015-10-10
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2021-03-31
  • 2019-07-17
相关资源
最近更新 更多