简体   繁体   中英

SQL Server stored procedure with 2 input parameters and 8 output parameters

I am trying to run a SQL Server stored procedure from ASP.NET & C# with 2 input and 8 output parameters like this:

public List<BarcodeClass> CheckTagId(int tagId, string deviceId)
{
    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);

            SqlParameter parameterTwo = new SqlParameter("@DeviceId", SqlDbType.NVarChar)
            {
                        Direction = ParameterDirection.Input,
                        Value = deviceId
            };
            command.Parameters.Add(parameterTwo);

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

            SqlParameter output2 = new SqlParameter(@"L2R", SqlDbType.NChar);
            output2.Direction = ParameterDirection.Output;
            command.Parameters.Add(output2);

            SqlParameter output3 = new SqlParameter(@"L3R", SqlDbType.NChar);
            output3.Direction = ParameterDirection.Output;
            command.Parameters.Add(output3);

            SqlParameter output4 = new SqlParameter(@"L4R", SqlDbType.NChar);
            output4.Direction = ParameterDirection.Output;
            command.Parameters.Add(output4);

            SqlParameter output5 = new SqlParameter(@"LD1R", SqlDbType.NChar);
            output5.Direction = ParameterDirection.Output;
            command.Parameters.Add(output5);

            SqlParameter output6 = new SqlParameter(@"LD2R", SqlDbType.NChar);
            output6.Direction = ParameterDirection.Output;
            command.Parameters.Add(output6);

            SqlParameter output7 = new SqlParameter(@"LD3R", SqlDbType.NChar);
            output7.Direction = ParameterDirection.Output;
            command.Parameters.Add(output7);

            SqlParameter output8 = new SqlParameter(@"LD4R", SqlDbType.NChar);
            output8.Direction = ParameterDirection.Output;
            command.Parameters.Add(output8);

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {

            }
        }
    }
    finally
    {
        connection.Close();
    }

    return barcodes;
}

Is this correct? When I run this code, I just get this error message:

An error has occurred

and nothing else....weird. Anyways this code appears not to be working, I can't debug this because the database can only be accessed to the server I push it too.

Here is the stored procedure...

declare @TG int
declare @DeviceId nvarchar(30)
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
set @DeviceId = 'testunitid'

exec uspCheckTagId @TG, @DeviceId, @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

I also do not have access to the database, so I cannot change the stored procedure.

I have this other stored procedure like so:

declare @sp varchar(30)
set @sp ='marksiphoneid'
exec uspCheckDeviceID @sp

and when I run this function:

 public List<BarcodeClass> CheckTagId(int tagId, string deviceId)
        {
            barcodes = new List<BarcodeClass>();
            try
            {
                using (connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand("uspCheckDeviceID", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        SqlParameter parameter = new SqlParameter("@DeviceId", SqlDbType.NVarChar)
                        {
                            Direction = ParameterDirection.Input,
                            Value = deviceId
                        };
                        command.Parameters.Add(parameter);


                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {

                        }
                    }
                }
            }
            finally
            {
                connection.Close();
            }

            return barcodes;
        }

this returns:

<ArrayOfBarcodeClass xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebServiceAPI.Models"/>

and these stored procedures are very similar.

The stored procedure does not have any output params specified so this won't work. The parameters in the SPROC would need to be declared like this:

declare @L1R nchar(10) output

And without being able to change that, this won't work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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