简体   繁体   English

ASP.NET存储过程方法不起作用

[英]ASP.NET Stored Procedure method not working

I have this method that is calling a stored procedure: 我有这种方法正在调用存储过程:

public List<BarcodeClass> CheckTagId(int tagId)
{
    barcodes = new List<BarcodeClass>();
    try
    {
        using (SqlCommand command = new SqlCommand("uspCheckTagId", connection))
        {
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@TG", SqlDbType.Int)
            {
                Direction = ParameterDirection.Input,
                Value = tagId
            };
            command.Parameters.Add(parameter);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
            }
        }
    }
    finally
    {
        connection.Close();
    }
    
    return barcodes;
}

My problem is this does not work. 我的问题是这行不通。 I just get this message: An error has occurred. 我刚收到此消息:发生错误。 I know its not the connection to the database because if I change the stored procedures name to another stored procedure it doesnt return that error message. 我知道它不是与数据库的连接,因为如果我将存储过程名称更改为另一个存储过程,它不会返回该错误消息。 I have a feeling it has something to do with this part: 我觉得这与这部分有关:

SqlParameter parameter = new SqlParameter("@TG", SqlDbType.Int)
{
    Direction = ParameterDirection.Input,
    Value = tagId
};

because I am passing in an int, if I use another stored procedure with a NVarChar and pass in a string this works! 因为我要传入一个int值,所以如果我将另一个存储过程与NVarChar一起使用并传入一个字符串,则可以正常工作! this there something wrong with the way I am passing in the int as parameter? 我将int作为参数传递的方式有问题吗?

Here is the stored procedure: 这是存储过程:

    declare @TG int

    declare @L1R nchar(10) -- Label1 Return Variable
    declare @L2R nchar(10) -- Label2 Return Variable
    declare @L3R nchar(10) -- Label3 Return Variable
    declare @L4R nchar(10) -- Label4 Return Variable
    declare @LD1R nchar(15) -- LData1 Return Variable
    declare @LD2R nchar(15) -- LData2 Return Variable
    declare @LD3R nchar(15) -- LData3 Return Variable
    declare @LD4R nchar(15) -- LData4 Return Variable
    set @TG = 10001
    exec uspCheckTagId @TG, @L1 = @L1R output, @L2 = @L2R output, @L3 = @L3R output, @L4 = @L4R output,  @LD1 = @LD1R output, @LD2 = @LD2R output, @LD3 = @LD3R output, @LD4 = @LD4R output
    print @L1R
    print @L2R
    print @L3R
    print @L4r
    print @LD1R
    print @LD2R
    print @LD3R
    print @LD4r

Please help! 请帮忙! I can't debug this because the database only works on the server I am pushing to and I dont have access to this server. 我无法调试此消息,因为该数据库仅在我要推送到的服务器上工作,而我无权访问此服务器。

The syntax of creating a SP in sql server is: 在sql server中创建SP的语法为:

CREATE PROCEDURE [schema].[procName]
    @param1 int, 
    @param2 bit
AS
BEGIN
    -- SP Logic
    declare @someVar int, -- note this is a variable, not a parameter.
            @anotherVar varchar(10)

    -- select, updates, deletes, etc
END

What you've posted as your proc definition (which was incomplete) does not have a parameter name @TG , but rather a variable. 您发布为proc定义(不完整)的内容没有参数名称@TG ,而是变量。 If you correct your proc to follow the above example, using parameters rather than variables, that should solve your immediate problem. 如果您使用参数而不是变量将过程更正为遵循上述示例,则应该可以解决您的直接问题。

Additionally, based on your code comments, it looks like you're attempting to use output parameters. 此外,根据您的代码注释,您似乎正在尝试使用输出参数。 those need to be set up in the SP declaration (just as normal parameters) but with the out or output modifier. 需要在SP声明中设置这些参数(就像正常参数一样),但要使用outoutput修饰符。

It seems you want something like this: 看来您想要这样的东西:

create procedure mySpName
    @TG int, -- input parameter
    @L1R nchar(10) out, -- output parameter -- Label1 Return Variable
    @L2R nchar(10) out, -- Label2 Return Variable
    @L3R nchar(10) out, -- Label3 Return Variable
    @L4R nchar(10) out, -- Label4 Return Variable
    @LD1R nchar(15) out, -- LData1 Return Variable
    @LD2R nchar(15) out, -- LData2 Return Variable
    @LD3R nchar(15) out, -- LData3 Return Variable
    @LD4R nchar(15) out -- LData4 Return Variable
AS
BEGIN
    -- SP body
END

To use the parameters in your c# code you could use the example: 要在c#代码中使用参数,可以使用以下示例:

Add the parameter: 添加参数:

SqlParameter output = new SqlParameter("@L1R", SqlDbType.NVarChar);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);

Read the parameter: 读取参数:

string L1R = cmd.Parameters["@L1R"].Value.ToString();

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

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