First, when creating a stored procedure in sqlserver, there are parameters and no parameters in the call. First, let's briefly introduce the absence of parameters:
Assume that the stored procedure is as follows:
Copy codeThe Code is as follows:
Create proc selectall
As
Select * from studentinf
The call of this sp is as follows:
Sqlcommand selectcmd = new sqlcommand ("selectall", conn );
// Conn is sqlconnection
Selectcmd. commandtype = commandtype. storedprocedure;
To add a result set to a dataadapter, you can:
Sqldataadapter studa = new sqldataadapter ();
Studa. selectcommand = selectcmd;
If there is a parameter: create proc andselect
@ Studentid varchar (10 ),
@ Studentname varchar (10 ),
As
Select * from studentinf where studentid = @ studentid and studentname = @ studentname
You can add the following parameters:
Selectcmd. parameters. add ("@ studentid", sqldbtype. nvarchar, 10 );
Selectcmd. parameters. add ("@ studentname", sqldbtype. nvarchar, 10 );
If there is only one parameter, you can assign a value as follows:
Sqlparameters onepara = selectcmd. parameters. add ("@ studentid", sqldbtype. nvarchar, 10 );
Onepara. value = "a string"