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!