Use the SQLHelper class to call stored procedures with output and return parameters

Source: Internet
Author: User
Notes for using SqlHelper (stored procedures with output and return parameters ):

In SqlHelper, if your stored procedure parameters include output and return parameters. Some functions cannot be used. Specifically:

        In SQLHelp, if the input parameters include params SqlParameter [] commandParameters, it is okay. The problem is that the input parameters include params object [] parameterValues:

For example:
Public static int ExecuteNonQuery (string connectionString, CommandType commandType, string commandText, params SqlParameter [] commandParameters)This is not mentioned.

This
Public static int ExecuteNonQuery (string connectionString, string spName, params object [] parameterValues)
Problems mentioned.

The following is a list of functions with these problems:

Public static int ExecuteNonQuery (string connectionString, string spName, params object [] parameterValues)
Public static int ExecuteNonQuery (SqlConnection connection, string spName, params object [] parameterValues)
Public static int ExecuteNonQuery (SqlTransaction transaction, string spName, params object [] parameterValues)

Public static DataSet ExecuteDataset (string connectionString, string spName, params object [] parameterValues)
Public static DataSet ExecuteDataset (SqlConnection connection, string spName, params object [] parameterValues)
Public static DataSet ExecuteDataset (SqlTransaction transaction, string spName, params object [] parameterValues)

Public static SqlDataReader ExecuteReader (string connectionString, string spName, params object [] parameterValues)
Public static SqlDataReader ExecuteReader (SqlConnection connection, string spName, params object [] parameterValues)
Public static SqlDataReader ExecuteReader (SqlTransaction transaction, string spName, params object [] parameterValues)

Public static object ExecuteScalar (string connectionString, string spName, params object [] parameterValues)
Public static object ExecuteScalar (SqlConnection connection, string spName, params object [] parameterValues)
Public static object ExecuteScalar (SqlTransaction transaction, string spName, params object [] parameterValues)

Public static XmlReader ExecuteXmlReader (SqlConnection connection, string spName, params object [] parameterValues)
Public static XmlReader ExecuteXmlReader (SqlTransaction transaction, string spName, params object [] parameterValues)

Public static void FillDataset (string connectionString, string spName,
  DataSet dataSet, string [] tableNames,
  Params object [] parameterValues)
Public static void FillDataset (SqlConnection connection, string spName,
  DataSet dataSet, string [] tableNames,
  Params object [] parameterValues)
Public static void FillDataset (SqlTransaction transaction, string spName,
  DataSet dataSet, string [] tableNames,
  Params object [] parameterValues)

 

The features of functions are as follows:
Params object [] parameterValues

If you need to use these functions, you will see that you cannot receive the returned Stored Procedure output and returned parameters.

Cause:
     In the internal implementation of these functions, he converts object [] parameterValues to SqlParameter [] commandParameters, while commandParameters is a local variable. After leaving this function, these cannot be accessed again. Before return, SqlHelper does not convert the content of commandParameters to parameterValues. Therefore, you cannot access the returned or output parameter values.

See the implementation of one of the functions:
Public static XmlReader ExecuteXmlReader (SqlConnection connection, string spName, params object [] parameterValues)
{
    If (connection = null) throw new ArgumentNullException ("connection ");
    If (spName = null | spName. Length = 0) throw new ArgumentNullException ("spName ");
    If (parameterValues! = Null) & (parameterValues. Length> 0 ))
   {
         SqlParameter [] commandParameters = SqlHelperParameterCache. GetSpParameterSet (connection, spName );
         AssignParameterValues (commandParameters, parameterValues );
         Return ExecuteXmlReader (connection, CommandType. StoredProcedure, spName, commandParameters );
   }
   Else
   {
         Return ExecuteXmlReader (connection, CommandType. StoredProcedure, spName );
   }
}

       I have never noticed this before. In the code written a few days ago, the parameters in the stored procedure are always unable to be returned. during debugging, I never thought about SqlHelper's situation. It took me a long time to find the problem and it was very depressing. Take it for granted. Debugging took a detour most of the time.

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.