简体   繁体   English

存储过程什么也不返回

[英]Stored Procedure returns nothing

This might be a simple fail on my part, but I just can't figure out where or how. 就我而言,这可能是一个简单的失败,但我只是不知道在哪里或如何做。 I've been coding a windows service that is doing a bunch of things. 我一直在编码Windows服务,该服务正在做很多事情。 One of which is inserting and getting data from a MS Sql 2005 database through stored procedures. 其中之一是通过存储过程从MS Sql 2005数据库插入数据并从中获取数据。

The following code is part of a windows service and now also a windows form, where both produce the same empty result. 下面的代码是Windows服务的一部分,现在也是Windows窗体的一部分,两者都产生相同的空结果。

    try
        {
            SqlCommand cmd = new SqlCommand("U_RfId_ProductNumberGet", connectionRFID);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //SqlParameter paramProd = new SqlParameter();
            SqlParameter paramOut = new SqlParameter();
            paramOut.ParameterName = "@ProductInformation";
            paramOut.Direction = System.Data.ParameterDirection.Output;
            paramOut.SqlDbType = System.Data.SqlDbType.VarChar;
            paramOut.Size = 50;
            cmd.Parameters.Add(paramOut);
            cmd.Parameters.AddWithValue("@ProductNumber", content); //content = "1" for testing
            connectionRFID.Open();

            textBox1.Text = (String)paramOut.Value;

            //cmd.Parameters["@ProductInformation"].Value.ToString();
            connectionRFID.Close();
        }
        catch (Exception ex)
        { textBox1.Text = ex.Message;
        connectionRFID.Close();
        }

And then there's the SP the code is calling. 然后是代码正在调用的SP。 I've tried changing it to only return a resultset instead of a scalar output parameter and then the call to the SP works, but I'd prefer to use the scalar values. 我尝试将其更改为仅返回结果集而不是标量输出参数,然后调用SP即可,但是我更喜欢使用标量值。

CREATE PROCEDURE U_RfId_ProductNumberGet
@ProductInformation     varchar(50) OUTPUT,
@ProductNumber          varchar(8)

AS
BEGIN TRAN
SET NOCOUNT ON;
BEGIN
SELECT @ProductInformation
                = CAST(vareNummer AS varchar(10)) 
                + '-' + CAST(vareTekst AS varchar(30))
FROM VareNummerVareTekst
WHERE ProductNumber = @ProductNumber
END
COMMIT TRAN

As a side note: If I execute the SP through SQL Management Studio I get a valid result. 附带说明:如果通过SQL Management Studio执行SP,则会得到有效结果。

Anyone notice what I've forgotten? 有人注意到我忘记了什么吗?

You forgot to execute the command. 您忘记了执行命令。

cmd.Execute(); // to get a resultset

or 要么

cmd.ExecuteNonQuery(); // to get output parameters but no resultset 

should do it depending on whether or not you want a resultset. 应该根据您是否想要结果集来执行此操作。

you have to use ExecuteNonQuery on command object. 您必须在命令对象上使用ExecuteNonQuery。 SqlCommand Executenonquery SqlCommand Executenonquery

    connectionRFID.Open();
    cmd.ExecuteNonQuery(); // this missing from your code 
   textBox1.Text = (String)paramOut.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM