sql - C# confirm table row has been deleted from database -


i've written method try , delete row db table based on primary key id. problem have try block returning "success" if record has been deleted / or doesn't exist.

public string delete_visit(int id)         {             string deleteresponse = null;             string cnn = configurationmanager.connectionstrings[connname].connectionstring;             using (sqlconnection connection = new sqlconnection(cnn))             {                 string sql = string.empty;                 sql = "delete [" + dbname + "].[dbo].[" + tbname + "]  visitornumber = @idnumber ";                  using (sqlcommand command = new sqlcommand(sql, connection))                 {                     command.parameters.add("@idnumber", sqldbtype.int);                     command.parameters["@idnumber"].value = id;                     try                     {                         connection.open();                         command.executenonquery();                          deleteresponse = "success";                      }                     catch (exception ex)                     {                         deleteresponse = "there problem deleting visit database. error message: " + ex.message;                     }                 }              }              return deleteresponse;         } 

i want able tell if row affected. can in sql server management studio so:

delete visits visitornumber=88; if @@rowcount = 0 print 'warning: no rows updated'; 

so want know how plug in @@rowcount bit c# can tell if row deleted?

thanks

executenonquery() returns int, indicating how many rows affected.

so:

int rowsaffected = command.executenonquery(); if (rowsaffected == 0) {     deleteresponse = "no rows affected"; } 

the problem number can influenced based on query does. executing triggers or calling stored procedures mess output, changing affected number of rows. if must, first execute query check record given id exists.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -