.Net下調(diào)用SqlServer2k中存儲過程
發(fā)表時間:2024-06-14 來源:明輝站整理相關軟件相關文章人氣:
[摘要]首先,在SqlServer中創(chuàng)建存儲過程,在調(diào)用時分為有參數(shù)和沒有參數(shù)兩種情況, 先就簡單的沒有參數(shù)的情況簡要的介紹: 假設存儲過程如下: CREATE PROC SelectAll AS SELECT * FROM StudentInf 則此sp的調(diào)用如下:SqlComma...
首先,在SqlServer中創(chuàng)建存儲過程,在調(diào)用時分為有參數(shù)和沒有參數(shù)兩種情況,
先就簡單的沒有參數(shù)的情況簡要的介紹:
假設存儲過程如下: CREATE PROC SelectAll
AS
SELECT * FROM StudentInf
則此sp的調(diào)用如下:
SqlCommand selectCMD = new SqlCommand(“SelectAll”, conn);
//conn 為SqlConnection
selectCMD.CommandType = CommandType.StoredProcedure;
如果需要將結果集加到某個DataAdapter上,則可如下:
SqlDataAdapter stuDA = new SqlDataAdapter();
stuDa.SelectCommand = selectCMD;
如果有參數(shù):create proc andSelect
@StudentId varchar(10),
@StudentName varchar(10),
As
Select * from StudentInf where StudentId = @StudentId and StudentName = @StudentName
則參數(shù)可以如下添加:
selectCMD.Parameters.Add(“@StudentId”, SqlDbType.NVarChar, 10);
selectCMD.Parameters.Add(“@StudentName”, SqlDbType.NvarChar, 10);
如果只有一個參數(shù),也可以這樣賦值:
SqlParameters onePara = selectCMD.Parameters.Add(“@StudentId”, SqlDbType.NVarChar, 10);
onePara.Value = “ a string ”