C#: OleDbCommand Update query not working?

Pow4Pow5

I have a database with multiple rows and columns with default value of 0. The user have to select a column by typing in the column name in a textBox and the system will automatically increment the value in the column by 1.

using (OleDbCommand cmd2 = connection.CreateCommand())
{
    int i = 1;
    cmd2.CommandText = "Update NormalRoom1 SET @time = @time + @b where [Appointment_Date]= @date";
    // add parameters
    cmd2.Parameters.AddRange(new OleDbParameter[]
    {
        // a is a string variable that holds a value the user chooses
        new OleDbParameter("@time", a),
        new OleDbParameter("@b", i),
        new OleDbParameter ("@date", dateTimePicker2.Value.ToString("dd/MM/yyyy"))
    });
    // execute
    cmd2.ExecuteNonQuery();
}
nvoigt

Only values can be variables. Your @time would be a column name and that cannot be a variable. As much as I hate to say this: you will need to insert that part by text-format.

Please be extra careful when doing so, it's an attack vector for sql injection. Best scenario is you let the user pick from a combobox and then check his selection against an internal list again.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事