Depending on the type of the returned value, we can divide the stored procedure into three types: the stored procedure of the returned record set, the stored procedure of the returned value (also known as the scalar stored procedure), and the behavior stored procedure.
(1) The code for executing a stored procedure without parameters is as follows:
SqlConnection conn = new SqlConnection ("connectionString ");
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = new SqlCommand ();
Da. SelectCommand. Connection = conn;
Da. SelectCommand. CommandText = "NameOfProcedure ";
Da. SelectCommand. CommandType = CommandType. StoredProcedure;
Then, you only need to select an appropriate method to execute this process for different purposes.
(2) The code for executing a stored procedure with parameters is as follows (we can declare the function that calls the stored procedure as ExeProcedure (string inputdate )):
SqlConnection conn = new SqlConnection ("connectionString ");
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = new SqlCommand ();
Da. SelectCommand. Connection = conn;
Da. SelectCommand. CommandText = "NameOfProcedure ";
Da. SelectCommand. CommandType = CommandType. StoredProcedure;
(The code above is the same, and the code to be added is as follows)
Param = new SqlParameter ("@ ParameterName", SqlDbType. DateTime );
Param. Direction = ParameterDirection. Input;
Param. Value = Convert. ToDateTime (inputdate );
Da. SelectCommand. Parameters. Add (param );
In this way, an input parameter is added.
To add output parameters:
Param = new SqlParameter ("@ ParameterName", SqlDbType. DateTime );
Param. Direction = ParameterDirection. Output;
Param. Value = Convert. ToDateTime (inputdate );
Da. SelectCommand. Parameters. Add (param );
To obtain the return value of the parameter store process:
Param = new SqlParameter ("@ ParameterName", SqlDbType. DateTime );
Param. Direction = ParameterDirection. ReturnValue;
Param. Value = Convert. ToDateTime (inputdate );
Da. SelectCommand. Parameters. Add (param );
From the code above, we can see that when there are many stored procedures or a large number of stored procedure parameters, this method will greatly affect the development speed. On the other hand, if the project is relatively large, therefore, the functions used for database logic will be a huge burden for future maintenance. Is there an improvement solution to this problem? You can call a stored procedure by passing in the name of a stored procedure when executing a stored procedure without parameters, in addition, in the SqlServer database, we can directly input the "Stored Procedure name (parameter list)" string in the query analyzer to execute the stored procedure, can this idea be applied to applications?
Therefore, type the corresponding code in the compiler. These codes are modified based on the code that calls the stored procedure without parameters. The Code is as follows:
SqlConnection conn = new SqlConnection ("connectionString ");
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = new SqlCommand ();
Da. SelectCommand. Connection = conn;
Da. SelectCommand. CommandText = "NameOfProcedure ('para1', 'para2 ', para3 )";
Da. SelectCommand. CommandType = CommandType. StoredProcedure;
To make the code more representative, the first and second parameters of the stored procedure to be called are string types, and the third parameter is an integer type. After the execution, the expected results can be fully achieved!
Comparison of the two call Methods
Through comparison, we can see that the second method has a very obvious advantage, that is, it can improve the development speed, save development time, and the code is easy to maintain, to a certain extent, the system size is also reduced. However, since the processing of stored procedure parameters is general, this method cannot meet the requirements if you want to obtain output parameters or get the returned values of stored procedures. Even so, this method can, after all, reduce a lot of code for developers. If you do not need to obtain the output parameters and return values, you can "once and for all ". Therefore, in actual program development, this method has some practical value.
Although there are many similar introductions on the internet, I would like to have a better understanding of the stored procedure, solicit suggestions from netizens, and share some good ideas and ideas, it can provide development progress and efficiency faster.