c# - Updating rows on a table with condition -
i'm trying delete rows, starting bottom of table using condition, when conditions met want stop updating table , leave rest was. example, if last entry on table meets it, delete it, if 1 after not meet condition stop there, , exite loop. here's code got, deleting rows :
private void button1_click(object sender, eventargs e) { messagebox.show("a atualizar dados"); bool check = true; { string connectionstring = @"data source=.\wintouch;initial catalog=bbl;user id=sa;password=pa$$w0rd"; string querystring = string.empty; using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); querystring = "delete wgcdoccab serie ='1' , tipodoc ='fss' , contribuinte ='999999990' , datadoc = convert(varchar(10),(dateadd(dd, -2, getdate())),120)" sqlcommand command = new sqlcommand(querystring, connection); //command.connection.open(); command.executenonquery(); } using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); querystring = "select * wgcdoccab serie !='1' , tipodoc !='fss' , contribuinte !='999999990' , datadoc != convert(varchar(10),(dateadd(dd, -1, getdate())),120) "; using (sqlcommand command = new sqlcommand(querystring, connection)) using (sqldatareader reader = command.executereader()) { if (reader.hasrows) { check = true; } else { check = false; messagebox.show("dados apagados com sucesso"); } command.connection.close(); } } } while (check);
try example:
delete tablename id > ( select max(id) tablename condition = false )
for example, if want delete until value 4:
delete tablename id > ( select max(id) tablename tablename.value = 4 )
if table rows are:
|id|value| | 1| 7| | 2| 4|
then subselect 2 , no rows deleted. however, if rows are:
|id|value| | 1| 7| | 2| 4| | 3| 9| | 4| 1|
then subselect still return id of 2, , last 2 rows deleted.
Comments
Post a Comment