1

我写了一个简单的程序,它调用带有 2 个参数(一个输入/一个输出)的存储过程,代码执行没有错误,但输出参数的值没有显示在输出窗口中!我的存储过程:

CREATE PROCEDURE [dbo].[sp_return_orders_by_employeeid_and_show_order_count]
(
@empid int,
@ordercount int=0 output
)
as
select *
from Orders
where orders.EmployeeID=@empid;
select *
from Orders
where Orders.EmployeeID=@empid
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”的值不显示!感谢您提供有用的答案。

4

6 回答 6

3

我认为您正在使用 SQL Server(通过查看您的 C# 代码),因此您不应该返回该值,您只需将输出变量设置为您想要返回的值,如下所示:

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
于 2012-02-14T09:53:39.237 回答
2

根据塞巴斯蒂安的回答,在返回之前分配@ordercount。

或者,由于您也RETURN @ordercount来自您的 SPROC,您也可以使用ParameterDirection.ReturnValue

这里

于 2012-02-14T09:54:17.433 回答
1

您的存储过程实际上并未为此输出参数分配任何值。您需要更改存储过程并执行类似 select @ordercount = count(*) 的操作

希望有帮助。

于 2012-02-14T09:53:44.187 回答
1

RETURN 不会设置@ordercount(它设置一个特殊的参数)

你需要这样的东西来返回结果记录数

...
as
select *
from Orders
where orders.EmployeeID=@empid;
SET @ordercount = @@ROWCOUNT;
GO
于 2012-02-14T09:57:57.673 回答
0

我不确定您是否可以返回@ordercount。

如果您想在订购时获得计数。您应该将存储过程修改为follwinf

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

-- Modify here
select @ordercount = Count(1)
from Orders
where Orders.EmployeeID=@empid
于 2012-02-14T09:55:07.550 回答
0

我解决了我的问题。我修改存储过程如下:

CREATE PROCEDURE sp_return_orders_by_employeeid_and_show_order_count
(
    @empid int,
    @ordercount int=0 output
)
as
select *
from Orders
where orders.EmployeeID=@empid;
select @ordercount=COUNT(*)
from Orders
where Orders.EmployeeID=@empid

而在 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();
            }
            }
    }
}

非常感谢您的有用答案。

于 2012-02-14T17:51:51.200 回答