0

ご協力いただきありがとうございます。みなさん(文字通り)

エントリを更新しているコードの別の部分を確認し、そのコードを使用して変更しました。今それは動作します

ここにあります

string sql = "DELETE from Login WHERE UserName = '" + comboBox1.SelectedItem.ToString() + "'";

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;


            cmd.Parameters.Add(new SqlParameter("@UserName", comboBox1.SelectedItem.ToString()));
            int rowdel = cmd.ExecuteNonQuery();
            MessageBox.Show("Done!");

これは、コンボボックスで選択されたデータベースから特定の行を削除するための私のコードです。私の先生はこれと同じコードを使用し、彼のプログラムは機能しました。ただし、次のエラーが表示されます。

"int rowInserted = cmd.ExecuteNonQuery();"

それは言う

タイプ'System.Data.SqlClient.SqlException'の未処理の例外がSystem.Data.dllで発生しました追加情報:'='の近くの構文が正しくありません。

これが私のコードです:

private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Server = HP-PC\\SQLExpress; Database = CProject; Trusted_Connection = True";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connectionString;
            conn.Open();

            string sql = "delete from [Login] where UserName = " + comboBox1.SelectedText.ToString();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;

            int rowInserted = cmd.ExecuteNonQuery();
            label7.Text = rowInserted.ToString();

            conn.Close();


        }

        private void AddDeleteUsers_Load(object sender, EventArgs e)
        {
            string connectionstring = "Server=HP-PC\\SQLExpress;Database=CProject;Trusted_Connection=True;";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connectionstring;
            conn.Open();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select UserName from Login";

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            comboBox1.Items.Add(reader["UserName"].ToString());
            reader.Close();

            conn.Close();
        }
4

5 に答える 5

4

ヒントをあげましょう。

エラーメッセージは、SQLコードの次の行にあります。

    string sql = "delete from [Login] where UserName = " + comboBox1.SelectedText.ToString();

'text'問題は、SQLが...などの単一引用符で囲まれたテキストを必要とすること です。

答えを提供せずに(これは宿題なので)これはあなたにそれを理解するのに十分だと思いますか?

「人気のある需要によって」編集

ちなみに、HTML入力(ComboBox)から値を取得することが推奨されない理由の1つは、悪意のある人物によって操作され、SQLインジェクションと呼ばれるSQLコードに置き換えられる可能性があるためです。

これを回避するには、パラメータの使用をお勧めします。

例は

    string sql = "delete from [Login] where UserName = @UserName"

次に、コマンドを実行する前に、コマンドにパラメーターを追加する必要があります。

command.Parameters.AddWithValue("@UserName", comboBox1.SelectedText.ToString());

これにより、誰かがSQLステートメントを不吉なものに変更するのを防ぐことができます。

于 2013-03-18T17:03:17.753 に答える
2

変化する

string sql = "delete from [Login] where UserName = " + comboBox1.SelectedText.ToString();

これに

string sql = "delete from [Login] where UserName = '" + comboBox1.SelectedText.ToString() + "'";

テキストを一重引用符で囲む必要があります。

于 2013-03-18T17:02:51.703 に答える
1

これを試して:

string sql = "delete from [Login] where UserName = '" + comboBox1.SelectedText.ToString() + "'";

SqlParameterSQLインジェクションを避けるために使用することをお勧めします。

        string sql = "delete from [Login] where UserName = @username";

        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("@username", comboBox1.SelectedText.ToString());

        int rowInserted = cmd.ExecuteNonQuery();
于 2013-03-18T17:02:52.597 に答える
1
string sql = "delete from [Login] where UserName = '" + comboBox1.SelectedText.ToString()+ "'";

いいですよね?

ちなみにSQLインジェクションには注意してください...

于 2013-03-18T17:03:04.397 に答える
0

SQLインジェクション攻撃を防ぐには、パラメータ化されたクエリを使用する必要があります。

    string connectionString = "Server = HP-PC\\SQLExpress; Database = CProject; Trusted_Connection = True";
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
        string sql = "delete from [Login] where UserName = @UserName;";

        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            SqlParameter p = new SqlParameter("UserName", comboBox1.SelectedText.ToString());
            cmd.Parameters.Add(p);
            cmd.ExecuteNonQuery();
        }
    }
于 2013-03-18T17:12:24.803 に答える