如何使用存储过程输出或返回参数来检索单个项
可以使用存储过程输出或返回参数来查找单个值。以下代码阐明了输出参数的用法:
- void GetProductNameUsingSPOutput( int ProductID, out string ProductName ) {
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind") )
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- { SqlCommand cmd = new SqlCommand("LookupProductNameSPOutput", conn );
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- cmd.CommandType = CommandType.StoredProcedure;
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- SqlParameter paramProdID = cmd.Parameters.Add("@ProductID", ProductID );
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- ParamProdID.Direction = ParameterDirection.Input;
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- SqlParameter paramPN = cmd.Parameters.Add("@ProductName", SqlDbType.VarChar, 40 );
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- paramPN.Direction = ParameterDirection.Output;
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- conn.Open();
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- cmd.ExecuteNonQuery();
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- ProductName = paramPN.Value.ToString();
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
-
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
-
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
-
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- } }
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
复制代码使用存储过程输出参数来检索单个值
<P/*Ëi£°nfhot.comvO{'ÐK <P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK1.建立一个 SqlCommand 对象来调用存储过程。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK2.通过将 SqlParameters 添加到 SqlCommand的 Parameters集合中,设置任何输入参数和单个输出参数。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK3.打开连接。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK4.调用 SqlCommand 对象的 ExecuteNonQuery方法。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK5.关闭连接。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK6.通过使用输出 SqlParameter 的 Value属性来检索输出值。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK <P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK上述代码使用了以下存储过程。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK@ProductID int,
<P/*Ëi£°nfhot.comvO{'ÐK@ProductName nvarchar(40) OUTPUT AS SELECT
<P/*Ëi£°nfhot.comvO{'ÐK@ProductName = ProductName FROM Products WHERE ProductID = @ProductID GO
<P/*Ëi£°nfhot.comvO{'ÐK以下代码阐明了如何使用返回值来指明是否存在特定行。从编码角度来看,这类似于使用存储过程输出参数,不同之处在于必须将 SqlParameter方向显式设置为 ParameterDirection.ReturnValue。
- bool CheckProduct( int ProductID ) {
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind") ){
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- SqlCommand cmd = new SqlCommand("CheckProductSP", conn );
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- cmd.CommandType = CommandType.StoredProcedure;
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- cmd.Parameters.Add("@ProductID", ProductID );
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- SqlParameter paramRet = cmd.Parameters.Add("@ProductExists", SqlDbType.Int );
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- paramRet.Direction = ParameterDirection.ReturnValue; conn.Open(); cmd.ExecuteNonQuery(); }
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
- return (int)paramRet.Value == 1; }
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK
<P/*Ëi£°nfhot.comvO{'ÐK
复制代码通过使用存储过程返回值来检查是否存在特定行
<P/*Ëi£°nfhot.comvO{'ÐK <P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK1.建立一个 SqlCommand 对象来调用存储过程。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK2.设置一个输入参数,该参数含有要访问的行的主键值。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK3.设置单个返回值参数。将一个 SqlParameter 对象添加到 SqlCommand的 Parameters集合中,并将其方向设置为 ParameterDirection.ReturnValue。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK4.打开连接。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK5.调用 SqlCommand 对象的 ExecuteNonQuery方法。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK6.关闭连接。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK7.通过使用返回值 SqlParameter 的 Value属性来检索返回值。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK <P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐK上述代码使用了以下存储过程。
<P/*Ëi£°nfhot.comvO{'ÐK<P/*Ëi£°nfhot.comvO{'ÐKCREATE PROCEDURE CheckProductSP
<P/*Ëi£°nfhot.comvO{'ÐK@ProductID int AS IF EXISTS( SELECT ProductID FROM Products WHERE ProductID =
<P/*Ëi£°nfhot.comvO{'ÐK@ProductID )
<P/*Ëi£°nfhot.comvO{'ÐKreturn 1 ELSE return 0 GO
<P/*Ëi£°nfhot.comvO{'ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。