Procedures stored in C # [closed]

0

I am scheduling a login in C # with user validations by Procedures stored in SQL Server .

I'm trying everything and it does not give me a result.

I would like to know if someone could help me with the code.

Clase de validacion en C#:

                try
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand("sp_ValidaUsuarioLogin_Id", conn);
                    command.CommandType = CommandType.StoredProcedure;

                    SqlParameter rept = new SqlParameter("rept", SqlDbType.Int);
                    rept.Direction = ParameterDirection.Output;
                    command.Parameters.Add(rept);

                    SqlParameter IdUsuario = new SqlParameter("IdUsuario", SqlDbType.Int);
                    IdUsuario.Direction = ParameterDirection.Output;
                    command.Parameters.Add(IdUsuario);

                    SqlParameter NomUsuario = new SqlParameter("NomUsuario", SqlDbType.VarChar);
                    NomUsuario.Direction = ParameterDirection.Output;
                    command.Parameters.Add(NomUsuario);

                    SqlParameter IdEmpresa = new SqlParameter("IdEmpresa", SqlDbType.Int);
                    IdEmpresa.Direction = ParameterDirection.Output;
                    command.Parameters.Add(IdEmpresa);

                    SqlParameter IdPais = new SqlParameter("IdPais", SqlDbType.VarChar);
                    IdPais.Direction = ParameterDirection.Output;
                    command.Parameters.Add(IdPais);

                    SqlParameter Pais = new SqlParameter("Pais", SqlDbType.VarChar);
                    Pais.Direction = ParameterDirection.Output;
                    command.Parameters.Add(Pais);

                    SqlParameter CodPais = new SqlParameter("CodPais", SqlDbType.VarChar);
                    CodPais.Direction = ParameterDirection.Output;
                    command.Parameters.Add(CodPais);

                    command.Parameters.AddWithValue("Usuario", mail);
                    command.Parameters.AddWithValue("PassUsuario", password);

                    int rowsAffected = command.ExecuteNonQuery();
                    var r = rowsAffected;
                    return Convert.ToInt32(command.Parameters["IdUsuario"].Value);
                }
                catch (Exception)
                {

                    throw;
                    return 5;
                }

and the stored procedure in SQL is:

ALTER PROCEDURE [dbo].[sp_ValidaUsuarioLogin_Id]
(
@Usuario  varchar(200),
@PassUsuario varchar(100),
@rept int output,
@IdUsuario int output,
@NomUsuario  varchar(300) output,
@IdEmpresa int output,
@IdPais varchar(6) output,
@Pais varchar(200) output,
@CodPais int output
)
as

declare @verifica int,
        @valusuario int

set @verifica   = (select count(*) from Usuario where email = @Usuario and estado = 'A' and claveHash =HASHBYTES('SHA2_512', @PassUsuario ) )
set @valusuario = (select count(*) from Usuario where email = @Usuario and estado = 'A')

if @verifica > 0
    begin
       set @rept = 1

       Select @IdUsuario=u.id_usuario,@NomUsuario=u.email,@IdEmpresa=e.id_Empresa,@IdPais=e.id_pais,@Pais=UPPER(p.nombre)
       From Usuario u
       Inner Join Empresa e On u.id_usuario=e.id_usuario
       Inner Join Pais p On e.id_pais=p.id_pais And p.id_idioma='ES'
       where u.email = @Usuario and u.estado = 'A' and u.claveHash =HASHBYTES('SHA2_512', @PassUsuario )

       Select @CodPais=id_grupo From GrupoCab Where nombre_corto=@IdPais And activo=1

    end
else
    begin
        if @valusuario > 0
            begin
            set @rept = 2
            set @IdUsuario=0
            set @NomUsuario = ''
            set @IdEmpresa=0
            set @IdPais=''
            set @Pais=''
            Set @CodPais=0
            end
        else
            begin
            set @rept = 0
            set @IdUsuario=0
            set @NomUsuario = ''
            set @IdEmpresa=0
            set @IdPais=''
            set @Pais=''
            Set @CodPais=0
        end
    end

Stored procedure

Class in C #

    
asked by Carlos Andrade 26.07.2018 в 03:55
source

1 answer

0

have you already tried the stored procedure inside SQL?

Something I have found on occasion is that even if the parameters are named correctly, if you do not add them in the same order to the parameters the call to the procedure fails.

also the type of "add" that you use affects, for the input I usually go with the add overload where you indicate the type of data and then add the value, in a single line something like this:

 comando.Parameters.Add(new SqlParameter("@para1", SqlDbType.NVarChar)).Value = "ParametrodeEntrada";

What results are you getting from the store procedure when you call it from c #?

    
answered by 26.07.2018 / 16:41
source