假设存储过程如下:
复制代码 代码如下:
create proc selectall
as
select * from studentinf
则此sp的调用如下:
sqlcommand selectcmd = new sqlcommand(“selectall”, conn);
//conn 为sqlconnection
selectcmd.commandtype = commandtype.storedprocedure;
如果需要将结果集加到某个dataadapter上,则可如下:
sqldataadapter studa = new sqldataadapter();
studa.selectcommand = selectcmd;
如果有参数:create proc andselect
@studentid varchar(10),
@studentname varchar(10),
as
select * from studentinf where studentid = @studentid and studentname = @studentname
则参数可以如下添加:
selectcmd.parameters.add(“@studentid”, sqldbtype.nvarchar, 10);
selectcmd.parameters.add(“@studentname”, sqldbtype.nvarchar, 10);
如果只有一个参数,也可以这样赋值:
sqlparameters onepara = selectcmd.parameters.add(“@studentid”, sqldbtype.nvarchar, 10);
onepara.value = “ a string ”