2012-02-14 5 views
1

私は2つのパラメータ(入力/出力)を持つストアドプロシージャを呼び出し、エラーは発生しませんが、出力パラメータの値は出力ウィンドウに表示されません! 私のストアドプロシージャ:私はC#から2つのパラメータ(入力と出力)を持つストアドプロシージャを呼び出しますが、出力パラメータの値は表示されません

CREATE PROCEDURE [dbo].[sp_return_orders_by_employeeid_and_show_order_count] 
(
@empid int, 
@ordercount int=0 output 
) 
as 
select * 
from Orders 
where [email protected]; 
select * 
from Orders 
where [email protected] 
return @ordercount 

とC#のコードの下に書いた:出力で

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 

namespace DB02 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SqlConnection connection = new SqlConnection(@"server=.\sqlexpress; 
      integrated security=true; database=northwind"); 
      try 
      { 
       connection.Open(); 
       SqlCommand cmd = connection.CreateCommand(); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.CommandText = "sp_return_orders_by_employeeid_and_show_order_count"; 
       SqlParameter inparam = cmd.Parameters.Add("@empid", SqlDbType.Int); 
       inparam.Direction = ParameterDirection.Input; 
       inparam.Value = 2; 
       SqlParameter outparam = cmd.Parameters.Add("@ordercount", SqlDbType.Int); 
       outparam.Direction = ParameterDirection.Output; 

       SqlDataReader rdr = cmd.ExecuteReader(); 
       while (rdr.Read()) 
       { 
        Console.WriteLine("{0}\t{1}\t{2}\t", rdr[0].ToString(), rdr[1].ToString(), 
         rdr[9].ToString()); 
       } 
       Console.WriteLine("Numbers of Orders= {0}", outparam.Value);   
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
      finally 
      { 
       connection.Close(); 
      } 

      } 
    } 
} 

: "outparam" の

値は表示されません!便利な回答ありがとうございます。

+0

ストアドプロシージャは実際には@ordercountに値を割り当てません –

答えて

3

私はあなたのC#コードを見て、SQL Serverを使用していると思いますので、値を返すべきではないので、出力変数を返す値に設定する必要があります。

SET @ordercount = 10; 

これにあなたのストアドプロシージャを変更してみてください:

CREATE PROCEDURE [dbo].[sp_return_orders_by_employeeid_and_show_order_count] 
(
    @empid INT, 
    @ordercount INT OUTPUT 
) 
AS BEGIN 

    SELECT @ordercount = COUNT(*) 
    FROM Orders 
    WHERE Orders.EmployeeID = @empid; 

END 
+0

この場合、データレコーダのレコードセットを返しません。 – gbn

+0

**あなたの答えは非常に便利です。 –

1

あなたのストアドプロシージャが実際にこの出力パラメータに任意の値を割り当てません。 ストアドプロシージャを変更してselect @ordercount = count(*)のようにする必要があります。

2

セバスチャンの答えによると、それを返す前に@ordercountを割り当ててください。

また、あなたのSPROCからもあなたRETURN @ordercountいるので、あなたもParameterDirection.ReturnValue

here

0

私はあなたが戻り@ordercountを行うことができますことを確認していない参照してください使用することができます。

ご注文の場合は数を取得したい場合は、あなたは

あなたが両方結果セットを返すために、このような何かを必要とするだろう(それは特別なパラメータを設定します)

CREATE PROCEDURE [dbo].[sp_return_orders_by_employeeid_and_show_order_count] 
(
@empid int, 
@ordercount int=0 output 
) 
as 
select * 
from Orders 
where [email protected]; 

-- Modify here 
select @ordercount = Count(1) 
from Orders 
where [email protected] 
1

RETURN follwinf @ordercountを設定しないであろうと、あなたが手続きを保存する変更する必要がありレコード数

... 
as 
select * 
from Orders 
where [email protected]; 
SET @ordercount = @@ROWCOUNT; 
GO 
0

私は私の問題を解決します。私は修正 は、次のようにストアドプロシージャ:

CREATE PROCEDURE sp_return_orders_by_employeeid_and_show_order_count 
(
    @empid int, 
    @ordercount int=0 output 
) 
as 
select * 
from Orders 
where [email protected]; 
select @ordercount=COUNT(*) 
from Orders 
where [email protected] 

そして、C#ソースコード、SqlDataReaderの実行後の密接な関連で。再度接続を開き、ExecuteNonQuery()を呼び出します。 出力パラメータに値があります。また、私はSqlParameter定義セクションを変更しますが、それは必要ありません。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 
namespace DB02 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SqlConnection connection = new SqlConnection(@"server=.\sqlexpress; 
      integrated security=true; database=northwind"); 
      try 
      { 
       connection.Open(); 
       SqlCommand cmd = connection.CreateCommand(); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.CommandText = "sp_return_orders_by_employeeid_and_show_order_count"; 
       //SqlParameter Definition Section. 
       SqlParameter inparam = new SqlParameter("@empid", SqlDbType.Int); 
       inparam.Direction = ParameterDirection.Input; 
       inparam.Value = 2; 
       cmd.Parameters.Add(inparam); 
       SqlParameter outparam = new SqlParameter("@ordercount", SqlDbType.Int); 
       outparam.Direction = ParameterDirection.Output; 
       cmd.Parameters.Add(outparam); 
       SqlDataReader rdr = cmd.ExecuteReader(); 
       while (rdr.Read()) 
       { 
        Console.WriteLine("{0}\t{1}\t{2}\t", rdr[0].ToString(), rdr[1].ToString(), 
         rdr[9].ToString()); 
       } 
       connection.Close(); 
       connection.Open(); 
       cmd.ExecuteNonQuery(); 
       Console.WriteLine("Numbers of Orders= {0}", outparam.Value);   
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
      finally 
      { 
       connection.Close(); 
      } 
      } 
    } 
} 

お返事ありがとうございます。

関連する問題