C # calls SQL in stored procedures that have an output parameter that returns information during the execution of a stored procedure

Source: Internet
Author: User

C # calls SQL with an output parameter in the stored procedure, and the type is a character type when you must specify the length of the parameter. Otherwise the obtained result is always only the first character. I was for this reason, tossing for a long time. In this record, for everyone to reference later!

For example:

CREATE PROCEDUREsp_accountrole_create@CategoryID int,@RoleName nvarchar(Ten),@Description nvarchar( -),@RoleID int Output asDECLARE @Count int--Find out if there is a record with the same nameSELECT @Count = Count(Roleid) fromAccount_roleWHERERoleName= @RoleNameIF @Count = 0INSERT  intoaccount_role (CategoryID, RoleName, Description)ValueS(@CategoryID,@RoleName,@Description)SET @RoleID = @ @IDENTITYRETURN 1 GO

SqlConnection DbConnection=new SqlConnection (mconnectionstring); SqlCommand Command=new SqlCommand ("Sp_accountrole_create", DbConnection);D bconnection.Open(connectstring);//the properties of the abandoned SqlCommand are stored procedures Command.commandtype=Commandtype.storedprocedure;command. Parameters.ADD("@CategoryID", SqlDbType.Int,4); command. Parameters.ADD("@RoleName", SqlDbType.NVarChar,Ten); command. Parameters.ADD("@Description", SqlDbType.NVarChar, -); command. Parameters.ADD("@RoleID", SqlDbType.Int,4);//The return value command. Parameters.ADD("returnvalue", SqlDbType.Int,4,//SizeParameterDirection.ReturnValue,false,//  isNullable0,//BytePrecision0,//byte scalestring. Empty,datarowversion.Default,NULL); Command.parameters["@CategoryID"]. value=permission. Categoryid;command.parameters["@RoleName"]. value=permission. Permissionname;command.parameters["@Description"]. value=permission. Description;//You can return a new ID value command.parameters["@RoleID"]. Direction=ParameterDirection.Output;introwsaffected=command. ExecuteNonQuery ();intResult=Command.parameters["ReturnValue"]. Value;int NewID =Command.parameters["@RoleID"]. value;

C # calls SQL in stored procedures that have an output parameter that returns information during the execution of a stored procedure

Related Article

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.