Sqlhelper calls the stored procedure to obtain the returned values and parameters

Source: Internet
Author: User

Sqlhelper extracted from Microsoft petshop. When a stored procedure is executed, the data parameter is always null.

At first, I thought my stored procedure was wrong. After the test, I found that there was no problem.

 

I also found some articles on the internet, saying that sqldatareader can be accessed after being disabled. Still unsolved

 

Later, I carefully read the sqlhelper code to find the problem, as shown below. After executing executereader, cmd. Parameters. Clear ();
The parameters are cleared. Of course, they cannot be obtained. You can finally go to bed.

        /// <summary>        /// Execute a SqlCommand that returns a resultset against the database specified in the connection string         /// using the provided parameters.        /// </summary>        /// <remarks>        /// e.g.:          ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));        /// </remarks>        /// <param name="connectionString">a valid connection string for a SqlConnection</param>        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>        /// <param name="commandText">the stored procedure name or T-SQL command</param>        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>        /// <returns>A SqlDataReader containing the results</returns>        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)        {            SqlCommand cmd = new SqlCommand();            SqlConnection conn = new SqlConnection(connectionString);            // we use a try/catch here because if the method throws an exception we want to             // close the connection throw code, because no datareader will exist, hence the             // commandBehaviour.CloseConnection will not work            try            {                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);                //cmd.Parameters.Clear();                return rdr;            }            catch            {                conn.Close();                throw;            }        }

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.