When you will need to use the ref and out
- Sometimes we need to get a value in the method of running the state, according to the methods defined, we can only get a return value, but sometimes we might want to get more value, can not be returned by the return value of such information, we can use ref or out parameters before, to obtain a plurality of return value.
- Sql stored in the implementation process, we can state by running the sql statement in a stored procedure returns the corresponding value of return .sql only supports Int return value format, we can get multiple return values by using the ref or out, and presented to a page
What is the difference between ref and out:
When the runtime code in the method, need to use the parameters and return values required by this parameter, we need to use ref.
If only to get multiple return values, and when they do not need to use these methods parameters, we can use out.
Examples
Example 1: Using ref and out pages asp.net
protected void the Page_Load ( Object SENDER, EventArgs E) { String str1, str2, S1 = " first parameter " , S2 = " second parameter " ; // run the following line of code will be prompted with the following error: // use unassigned local variable "str1" // UseRef (str1 REF, REF str2); // output: // first parameter out is // used out of the second parameter UseOut ( out str1, out str2) ; Response.Write (str1 + " a " ); Response.Write (str2 +" A " ); // output: // first parameter REF // second parameter REF UseRef ( REF S1, REF S2); Response.Write (S1 + " a " ); Response.Write (S2 + " a " ); } public void UseOut ( out String str1, out String str2) { str1 = " used out of the first parameter " ; str2 = "Out of the second parameter " ; } public void UseRef ( REF String S1, REF String S2) { S1 + = " Use REF " ; S2 + = " Use REF " ; }
Example 2: Process parameters stored using the output keyword corresponds to c # code, then is ref and out.
ref and out and output relationship is as follows:
ParameterDirection Value | description | The corresponding C # | The corresponding SQL |
Input | Entry | ||
InputOutput | input Output | ref | The corresponding output |
Output | Export | out | |
ReturnValue | return value |
Start example:
The establishment of the Employee table:
Data in the table below:
Build stored procedures as follows:
ALTER PROCEDURE dbo.InsertEmployee ( @EmployeeName nvarchar(20), @EmployeeAge int=null, @EmployeeDepartmentID int=null, @EmployeeScore int=null, @outValue nvarchar(20) output ) AS /* SET NOCOUNT ON */ if exists(select * from Employee where EmployeeName=@EmployeeName) begin set @outValue='用户名'+@EmployeeName+'重复!' return end insert into Employee (EmployeeName,EmployeeAge,EmployeeDeparmentID,EmployeeScore) values (@EmployeeName,@EmployeeAge,@EmployeeDepartmentID,@EmployeeScore) set @outValue='用户'+@EmployeeName+'建立成功!' return
Page code is as follows:
string connectionString = ConfigurationManager.ConnectionStrings["dbstconnectionstring"].ConnectionString; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "InsertEmployee"; cmd.Parameters.AddWithValue("@EmployeeName", "宋八"); cmd.Parameters.AddWithValue("@EmployeeAge", 20 is ); cmd.Parameters.AddWithValue ( " @EmployeeDepartmentID " , . 1 ); cmd.Parameters.AddWithValue ( " @EmployeeScore " , 95 ); // Note param_outValue.Direction // set param_outValue.Direction = Output, parameter need only be statement to // the corresponding database Output // the SqlParameter = new new param_outValue the SqlParameter ( "@ outValue", SqlDbType.NVarChar, 20 is); // param_outValue.Direction = ParameterDirection.Output; // cmd.Parameters.Add (param_outValue); // Note param_outValue.Direction // Set param_outValue.Direction = InputOutput, parameters need to be initialized // in the corresponding database Output the SqlParameter param_outValue = new new the SqlParameter ( " @outValue " , SqlDbType.NVarChar, 20 is ); param_outValue.Direction = ParameterDirection.InputOutput; param_outValue.Value = String . Empty; cmd.Parameters.Add (param_outValue); conn.Open (); the cmd.ExecuteNonQuery (); conn.Close (); Response.Write (param_outValue.Value);
The first time you run the output:
Song eight users build successful!
Second run output:
Username Song eight repeat!
This switched: https: //www.cnblogs.com/oneword/archive/2010/07/29/1787569.html