【问题标题】:Incorrect syntax inserting data into table将数据插入表中的语法不正确
【发布时间】:2013-10-20 15:32:49
【问题描述】:

我的update() 方法遇到了一些问题。这个想法是用户提供食谱名称、成分、说明,然后使用 Filestream 选择图像。

一旦用户点击“添加食谱”,这将调用更新方法,但就目前情况而言,我收到一个错误,提示文本框的内容:

这里是update()方法代码:

 private void updatedata()

        { 
        // filesteam object to read the image
        // full length of image to a byte array

            try
            {
                // try to see if the image has a valid path

                if (imagename != "")
                {

                    FileStream fs;
                    fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);

                    // a byte array to read the image

                    byte[] picbyte = new byte[fs.Length];
                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();

                    //open the database using odp.net and insert the lines

                    string connstr = @"Server=mypcname\SQLEXPRESS;Database=RecipeOrganiser;Trusted_Connection=True";

                    SqlConnection conn = new SqlConnection(connstr);
                    conn.Open();
                    string query;
                    query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) values (" + textBox1.Text + "," + " @pic" + "," + textBox2.Text + "," + textBox3.Text + ")";
                    SqlParameter picparameter = new SqlParameter();
                    picparameter.SqlDbType = SqlDbType.Image;
                    picparameter.ParameterName = "pic";
                    picparameter.Value = picbyte;
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.Parameters.Add(picparameter);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Image successfully saved");
                    cmd.Dispose();
                    conn.Close();
                    conn.Dispose();
                    Connection();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

谁能看到我在插入到食谱查询时出错的地方或建议这部分代码的替代方法?

【问题讨论】:

  • 请使用参数化查询..
  • 另外,将所有 RecipeIngredients 填充到单个列中也不是标准化设计,您应该使用 using 块。
  • 你没有引用你的文本值。在每个值之前和之后放置一个单引号(')。

标签: c# .net sql-server-2008


【解决方案1】:

您的代码对 SQL 注入开放,但您的错误可能来自某些包含单引号的文本(例如说明字段),这会破坏您使用连接用户输入的命令字符串构建。

编辑 正如有人在评论中指出的那样,错误是由文本框周围缺少引号引起的。但是,虽然很容易修复,但这并不是正确的方法,因为修复添加缺失引号的错误是错误的。这只是推迟问题,留下一个等待被利用的大安全漏洞。

参数化查询可以避免所有这些混乱。

  string connstr = "....";     
  string query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) " + 
          "values (@name, @pic, @ing, @instr)";
  using(SqlConnection conn = new SqlConnection(connstr))
  using(SqlCommand cmd = new SqlCommand(query, conn))
  {
    conn.Open();
    SqlParameter picparameter = new SqlParameter();
    picparameter.SqlDbType = SqlDbType.Image;
    picparameter.ParameterName = "@pic";
    picparameter.Value = picbyte;
    cmd.Parameters.Add(picparameter);
    cmd.Parameters.AddWithValue("@name", textbox1.Text);
    cmd.Parameters.AddWithValue("@ing", textbox2.Text);
    cmd.Parameters.AddWithValue("@instr", textbox3.Text);
    cmd.ExecuteNonQuery();
    MessageBox.Show("Image successfully saved");
  }

【讨论】:

  • 这是由于在这种情况下缺少周围的单引号。短语“Salmon Pasta”不包含单引号。他们仍然应该参数化其他三个值。
  • 是否应该有两个字符串查询;声明的 as one 已经在父作用域中声明了?
【解决方案2】:

由于您使用字符串连接,您可能错过了一个引号,或者您添加了一个额外的引号或错过了一个逗号或添加了额外的逗号等......

不要这样用!

您的错误看起来并不明显,但您应该始终使用parameterized queries。这种字符串连接对SQL Injection 攻击开放。

query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) values (@p1, @pic, @p3, @p4)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue(@p1, textBox1.Text);
cmd.Parameters.AddWithValue(@pic, textBox1.Text);
cmd.Parameters.AddWithValue(@p3, textBox1.Text);
cmd.Parameters.AddWithValue(@p4, picparameter);

【讨论】:

    【解决方案3】:

    试试这个

    query = "插入食谱(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) 值 ('" + textBox1.Text + "'," + "@pic" + ",'" + textBox2.Text + "',' " + textBox3.Text + "')";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多