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