简体   繁体   中英

Can't update MS Access database in C#?

My code:

OleDbCommand cmd1 = new OleDbCommand("UPDATE student_info SET fee_due = @fee_due WHERE adm_no = @adm_no", con);

cmd1.Parameters.AddWithValue("@adm_no", adm_no);
cmd1.Parameters.AddWithValue("@fee_due", fee_due);

int affect = cmd1.ExecuteNonQuery();

MessageBox.Show(affect.ToString());

My code always shows 0 row affected every time but in my database the are must be a row that will be affect

Can you suggest me how I debug this problem?

Since OleDB for MS Access doesn't support named parameters (only positional parameters), you must be very careful about providing the values in the same order in which you define the parameters.

In your case, the statement lists @fee_due first, before @adm_no - but you provide the values in the other order.

Change your code like this:

OleDbCommand cmd1 = new OleDbCommand("UPDATE student_info SET fee_due = @fee_due WHERE adm_no = @adm_no", con);

// provide the value for @fee_due FIRST    
cmd1.Parameters.AddWithValue("@fee_due", fee_due);

// provide the value for @adm_no only after @fee_due
cmd1.Parameters.AddWithValue("@adm_no", adm_no);

int affect = cmd1.ExecuteNonQuery();

MessageBox.Show(affect.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM