Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple values from a SQL Server stored procedure

What I would like to do is use my spLogin stored procedure to return two values from the same table, both of which I would like to save in sessions in C#.

Here is the table

create table tbClients
(
    ClientID int primary key identity(0,1),
    ClientFirstName varchar(20),
    ClientLastName varchar(20),
    ClientAddress varchar(60),
    ClientOrigin varchar(20),
    ClientUsername varchar(20),
    ClientPassword int,
    ClientSecurity int
)

When the client clicks the login button I want to code a procedure that will check to see if the user is valid, what their security level is, and that their first name is.

Here is what I have so far

create procedure spLogin(
    @ClientUsername varchar(20),
    @ClientPassword int
)
AS BEGIN
    DECLARE @Security int
    DECLARE @ClientFirstName varchar(20)        

    IF EXISTS (SELECT * FROM tbClients 
               WHERE ClientUsername = @ClientUsername 
                 AND ClientPassword = @ClientPassword)
    BEGIN               
        SELECT 
            @Security = ClientSecurity, 
            @ClientFirstName = ClientFirstName 
        FROM tbClients 
        WHERE
            ClientUsername = @ClientUsername 
            AND ClientPassword = @ClientPassword

        IF(@Security = 1)
        BEGIN
            SELECT 'Admin' as Security, @ClientFirstName
        END
        ELSE
        BEGIN
            SELECT 'Customer' as Security, @ClientFirstName
        END
    END
    ELSE
    BEGIN
        SELECT 'INVALID'
    END
END
GO

Don't know if this will work because I am not sure how to store these values in C# without using a dataset, which doesn't seem to be working so far?

like image 549
William Z. Foster Avatar asked Aug 09 '26 17:08

William Z. Foster


1 Answers

I would write this procedure a bit differently something like this.....

create procedure spLogin
    @ClientUsername     varchar(20)
   ,@ClientPassword     int
   ,@Security           VARCHAR(10)     OUTPUT
   ,@ClientFirstName    varchar(20)     OUTPUT
   ,@ValidLogin         INT             OUTPUT
AS 
BEGIN
  SET NOCOUNT ON;        

    IF EXISTS (SELECT * FROM tbClients 
               WHERE ClientUsername = @ClientUsername 
                 AND ClientPassword = @ClientPassword)
      BEGIN               
            SELECT @ValidLogin = 1 
                  ,@Security = CASE WHEN ClientSecurity = 1 
                                 THEN 'Admin' ELSE 'Customer' END
                  ,@ClientFirstName = ClientFirstName 
            FROM tbClients 
            WHERE ClientUsername = @ClientUsername 
              AND ClientPassword = @ClientPassword
      END
    ELSE
      BEGIN
            SET @ValidLogin = 0;
      END
END
GO

Not an expert of C# but you would handle the output parameters in C# something like....

// define connection and command, in using blocks to ensure disposal
using(SqlConnection conn = new SqlConnection(pvConnectionString ))
using(SqlCommand cmd = new SqlCommand("dbo.spLogin", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;

    // set up the input parameters
    cmd.Parameters.Add("@ClientUsername", SqlDbType.VarChar, 20);
    cmd.Parameters.Add("@ClientPassword", SqlDbType.Int);
    cmd.Parameters.Add("@Security", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@ClientFirstName", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@Success", SqlDbType.Int).Direction = ParameterDirection.Output;

    // set parameter values
    cmd.Parameters["@ClientUsername"].Value = UserNamTextbox.Text;

    // open connection and execute stored procedure
    conn.Open();
    cmd.ExecuteNonQuery();

    // read output value from @Security
    int Security = Convert.ToInt32(cmd.Parameters["@Security"].Value);

    if Security == 1 ....... and so on.......

    conn.Close();
}
like image 59
M.Ali Avatar answered Aug 12 '26 08:08

M.Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!