c# - MySQL - UPDATE query not effecting row -
i have update query not working doesn't show errors, no rows effected/ matched:
update `budget` set `amount` = 500, `rest` = 500 `company_number` = 1 , `section_number` = 1 , `chapter_number` = 1 , `article_number` = 3 , `subarticle_number` = 0 , `ssubarticle_number` = 0
but when "select" database got row returned:
select * budget `company_number` = 1 , `section_number` = 1 , `chapter_number` = 1 , `article_number` = 3 , `subarticle_number` = 0 , `ssubarticle_number` = 0
this "budget" table:
create table `budget` ( `budget_id` int(11) not null auto_increment, `company_number` int(11) not null default '0', `section_number` int(11) not null default '0', `chapter_number` int(11) not null default '0', `article_number` int(11) not null default '0', `subarticle_number` int(11) not null default '0', `ssubarticle_number` int(11) not null default '0', `name` varchar(90) default null, `amount` double default null, `rest` double default null, `insert_date` date default null, `transfer_date` date default null, `has_children` bit(1) default null, primary key (`budget_id`) ) engine=innodb auto_increment=16 default charset=utf8;
the method use run queries.
public bool updatetransfer(string companynumber, string sectionnumber, string transfernumber, string transferdate, string[] budgetrow, string oldamount, string newamount) { mysqltransaction tr = connection.begintransaction(); cmdquery = connection.createcommand(); cmdquery.connection = connection; cmdquery.transaction = tr; try { cmdquery.commandtext = @"set sql_safe_updates = 0;"; cmdquery.executenonquery(); // first, return old amount source row budget cmdquery.commandtext = @"update budget set `rest` = `rest` + " + oldamount + " `company_number` = " + companynumber + " , `section_number` = " + sectionnumber + " , `chapter_number` = " + budgetrow[0] + " , `article_number` = " + budgetrow[1] + " , `subarticle_number` = " + budgetrow[2] + " , `ssubarticle_number` = " + budgetrow[3]; cmdquery.executenonquery(); messagebox.show(cmdquery.commandtext); // second, make transfer , substract new amount source row budget cmdquery.commandtext = @"update `budget` set `rest` = `rest` - " + newamount + " `company_number` = " + companynumber + " , `section_number` = " + sectionnumber + " , `chapter_number` = " + budgetrow[0] + " , `article_number` = " + budgetrow[1] + " , `subarticle_number` = " + budgetrow[2] + " , `ssubarticle_number` = " + budgetrow[3]; cmdquery.executenonquery(); messagebox.show(cmdquery.commandtext); // third, update rest , new amount of destination row budget cmdquery.commandtext = @"update `budget` set `amount` = " + newamount + ", `rest` = " + newamount + " `company_number` = " + companynumber + " , `section_number` = " + sectionnumber + " , `chapter_number` = " + budgetrow[4] + " , `article_number` = " + budgetrow[5] + " , `subarticle_number` = " + budgetrow[6] + " , `ssubarticle_number` = " + budgetrow[7]; cmdquery.executenonquery(); messagebox.show(cmdquery.commandtext); // last step, update transfer table. cmdquery.commandtext = @"update `transfer` set `amount` = " + newamount + ", `transfer_date` ='" + transferdate + "'" + " `transfer_number` = " + transfernumber + " , `company_number` = " + companynumber + " , `section_number` = " + sectionnumber + cmdquery.executenonquery(); messagebox.show(cmdquery.commandtext); cmdquery.commandtext = @"set sql_safe_updates = 1;"; cmdquery.executenonquery(); tr.commit(); return true; } catch (mysqlexception ex) { messagebox.show("error " + ex.errorcode.tostring()); tr.rollback(); return false; } } }
here example on how can verify if there row changes after executing sql on c#
public static boolean performdatabaseaction(string sqlquery) { using (sqlconnection con = dbconnect()) // sqlconnection { sqlcommand cmd = new sqlcommand(sqlquery); cmd = new sqlcommand(sqlquery); commandtype = system.data.commandtype.text; connection = con; int rows = cmd.executenonquery(); // gets count of affected rows if(rows > 0) return true; else return false; } }
and query works try on one.
Comments
Post a Comment