Use sqldatareader to obtain output parameters

Source: Internet
Author: User
Finally, the problem of getting output parameters using sqldatareader is solved! The root cause is that you do not have a deep understanding of the sqldatareader data reader!

      Let's take a look at my example:      Create procedure h_transfer      (              @ Returnvalue varchar (10) Output      )      As        Declare @ isanswer char (2)        Select @ isanswer = isanswer from question        If (isanswer = 1)            Begin                Set @ returnvalu = 'handled'            End      Else (isanswer = 0)            Begin                  Set @ returnvalue = 'unprocessed'            EndAt first, I called it in the Program (I used a static method creatasdr in the main class) Sqlparameter [] paras = new sqlparameter [1]  Paras [0] = new sqlparameter ("@ returnvalue", sqldbtype. varchar, 10 ); Sqldatareader SDR = sqlhelpclass. Main. createsdr ("h_transfer", paras); If (SDR. Read ()){ This. textbox1.text = paras [0]. value. tostring ();} But in fact, an error will be reported saying "the instance is not referenced by the object", that is, this. textbox1.text does not get the value. The error is that when obtaining the output parameters or return type parameters in the stored procedure, we must disable the sqldatareader object before assigning values to the parameters of the output parameter or return value type.  The program should be changed to the following:        Sqlparameter [] paras = new sqlparameter [1];
        Paras [0] = new sqlparameter ("@ returnvalue", sqldbtype. varchar, 10 );
        Paras [0]. Direction = parameterdirection. output;
        Sqldatareader SDR = sqlhelpclass. Main. createsdr ("h_transfer", paras );
        SDR. nextresult (); // This is the key. It indicates that the previous packet has been read.                                        // When the SDR of the previous record is disabled
        Labstates. Text = paras [0]. value. tostring ();  In this way, we can get the output parameters and return value parameters.  This is not the case if datatable or dataset is used, because all the data is stored in the memory.    I also understood it by referring to the blog of a master on the Internet. Unfortunately, I did not remember the blog address of the master.    Well, let's do it first. If there are any new situations, I will tell you in 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.