c# - Simple way to check how many rows I have in my database -
i'm connected database through sqlconnection
class. there simple why check how many rows in database or have create sqldatareader
, increment till last row in ?
i assume "rows in database" means "rows in table".
you should use count
, sqlcommand.executescalar
:
int rowcount = 0; using(var con = new sqlconnection(connectionsstring)) using (var cmd = new sqlcommand("select count(*) dbo.tablename", con)) { try { con.open(); rowcount = (int) cmd.executescalar(); } catch (exception ex) { // log exception or else useful, otherwise it's better to... throw; } }
Comments
Post a Comment