首先在SqlServer中创建存储过程在调用时分为有参数和没有参数两种情况先就简单的没有参数的情况简要的介绍
假设存储过程如下 CREATE PROC SelectAll
AS
SELECT * FROM StudentInf
则此sp的调用如下
SqlCommand selectCMD = new SqlCommand(SelectAll conn);
//conn 为SqlConnection
selectCMDCommandType = CommandTypeStoredProcedure;
如果需要将结果集加到某个DataAdapter上则可如下
SqlDataAdapter stuDA = new SqlDataAdapter();
stuDaSelectCommand = selectCMD;
如果有参数create proc andSelect
@StudentId varchar()
@StudentName varchar()
As
Select * from StudentInf where StudentId = @StudentId and StudentName = @StudentName
则参数可以如下添加
selectCMDParametersAdd(@StudentId SqlDbTypeNVarChar );
selectCMDParametersAdd(@StudentName SqlDbTypeNvarChar );
如果只有一个参数也可以这样赋值
SqlParameters onePara = selectCMDParametersAdd(@StudentId SqlDbTypeNVarChar );
oneParaValue = a string