C # Number of affected rows returned by the Stored Procedure

Source: Internet
Author: User
Tags rowcount

After the update operation is completed in the stored procedure, @ rowcount is used to return the number of affected rows. If multiple statements exist, you can define the number of rows affected by variable storage. Example:

 

Alter procedure [DBO]. [p_csm_updatehandleresultempname] @ handleindex Int =-1 asbegin -- set nocount on added to prevent extra result sets from -- interfering with select statements. set nocount on; update a set. fromempname = B. empname from handleresult a left join users B on. fromusername = B. openid where. handleindex> @ handleindex and. fromempname is null and B. empname is not null select @ rowcount -- This returns the number of affected rows end

 

Then in the C # code, use sqlcommand. executescalar () to execute the stored procedure, you can get @ rowcount, that is, the number of affected rows.

The Code is as follows:

Private int updatehandleresultempname () {sqlconnection con = new sqlconnection ("your database connection string"); sqlcommand COM = con. createcommand (); COM. commandtype = commandtype. storedprocedure; COM. commandtext = "p_csm_updatehandleresultempname"; // The Name Of The Stored Procedure con. open (); int rowcount = convert. toint32 (COM. executescalar (); con. close (); Return rowcount ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.