.Net下调用SqlServer2k存储过程
2007-11-11 04:41:20 来源:WEB开发网核心提示:首先,在SqlServer中创建存储过程,.Net下调用SqlServer2k存储过程,在调用时分为有参数和没有参数两种情况,先就简单的没有参数的情况简要的介绍:假设存储过程如下: CREATE PROC SelectAllASSELECT * FROM StudentInf则此sp的调用如下:SqlCommand
首先,在SqlServer中创建存储过程,在调用时分为有参数和没有参数两种情况,
先就简单的没有参数的情况简要的介绍:
假设存储过程如下: 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 ”
先就简单的没有参数的情况简要的介绍:
假设存储过程如下: 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 ”
Tags:Net 调用 SqlServerk
编辑录入:coldstar [复制链接] [打 印]更多精彩
赞助商链接