0

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

レシピクエリへの挿入でどこが間違っているのか、またはコードのこの部分に対する代替アプローチを提案できる人はいますか?

4

3 に答える 3

3

あなたのコードは SQL インジェクションに対してオープンですが、おそらくエラーは一重引用符を含むテキスト (指示フィールドなど) から発生し、ユーザー入力の連結を使用してコマンド文字列のビルドを壊します。

EDIT 誰かがコメントで指摘したように、エラーはテキストボックスの周りに引用符がないことが原因です。しかし、修正は簡単ですが、不足している引用符を追加してエラーを修正するのは間違っているため、それは正しい方法ではありません。悪用されるのを待っている大きなセキュリティ ホールを残して、問題を先延ばししているだけです。

パラメータ化されたクエリは、この混乱をすべて回避できます。

  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");
  }
于 2013-10-20T15:36:28.000 に答える
3

文字列連結を使用しているため、おそらく引用符を逃したか、余分な引用符を入れたか、コンマを逃したか、余分なコンマを入れたなど....

このように使用しないでください。

エラーは明らかに見えませんが、常に使用する必要があります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);
于 2013-10-20T15:38:52.780 に答える