明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

.Net下調(diào)用SqlServer2k中存儲(chǔ)過程

[摘要]首先,在SqlServer中創(chuàng)建存儲(chǔ)過程,在調(diào)用時(shí)分為有參數(shù)和沒有參數(shù)兩種情況,  先就簡(jiǎn)單的沒有參數(shù)的情況簡(jiǎn)要的介紹:  假設(shè)存儲(chǔ)過程如下: CREATE PROC SelectAll AS  SELECT * FROM StudentInf  則此sp的調(diào)用如下:SqlComma...
首先,在SqlServer中創(chuàng)建存儲(chǔ)過程,在調(diào)用時(shí)分為有參數(shù)和沒有參數(shù)兩種情況,

  先就簡(jiǎn)單的沒有參數(shù)的情況簡(jiǎn)要的介紹:

  假設(shè)存儲(chǔ)過程如下:   CREATE PROC SelectAll



      AS

  SELECT * FROM StudentInf

  則此sp的調(diào)用如下:

SqlCommand selectCMD = new SqlCommand(“SelectAll”, conn);

  //conn 為SqlConnection

  selectCMD.CommandType = CommandType.StoredProcedure;


  如果需要將結(jié)果集加到某個(gè)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);


  如果只有一個(gè)參數(shù),也可以這樣賦值:

 SqlParameters onePara = selectCMD.Parameters.Add(“@StudentId”,                                         SqlDbType.NVarChar, 10);

  onePara.Value = “ a string ”