Search code examples
asp.net.netsql-serverado.netentity-framework-4

How to get number of rows updated by a query in .net


In my database, i have a trigger which insert the change log entries when a row in Table tblA is updated.

Now, in my code i have to update it through a plain Sql query like

int count = DBContext.ExecuteStoreCommand("<sql query to update records>");

This count variable contains the number of rows affected(no of rows updated + no of rows inserted) due to query.

So my question is, How do i can get only the number of updated rows?

Currently i'm using Entity framework 4. I have looked for solution through connected or disconnected model but couldn't help myself.


Solution

  • int count = DBContext.ExecuteStoreCommand("");

    I think you hv to change this to return Select result set

    then do this,

    <sql query to update> 
    Select @@RowCount rowcountAffected
    

    Or suppose your update is

    update table1 set col1='foo' where id=2
    select count(*) rowcountAffected from table1 where id=2
    

    The most efficient way to return row affected can be

    i) Assuming you only update (don't refresh any record after that)

    Put Set Nocount ON
    Declare @Output parameter inside proc