I need to create a Login with user types compulsorily with stored procedures in SQL Server. I have not been able to capture the user type id.
Types of users:
1 = Administrador
2 = Secretaria
3 = Profesional
The stored procedure is:
create procedure ident (@rut varchar(10), @contrasena varchar(30))
as
begin
select ID_TIPO_USUARIO from USUARIO
where RUT_USUARIO = @rut and CONTRASENA = @contrasena
end
And in VisualBasic I have the following
Dim rut, contrasena As String
rut = tbxRut.Text
contrasena = tbxContraseña.Text
Using cnn As SqlConnection = New SqlConnection("Initial Catalog=CONSULTORIO;" & _
"Data Source=DESKTOP-LGTPUJM\SQLEXPRESS;Integrated Security=SSPI;")
Try
cnn.Open()
cmd = New SqlCommand("ident", cnn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@rut", rut)
cmd.Parameters.AddWithValue("@contrasena", contrasena)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error")
End Try
End Using
The stored procedure executes without errors, but I don't know how to capture the query log
Note that you are using
ExecuteNonQuery
, which implies that you are executing a query that is not going to return a result.You can easily get the result of the query, using for example
ExecuteScalar()
, which will return you directlyID_TIPO_USUARIO
.Or if you want to get the full result, you should use
ExecuteReader()
.To get the answer you must use the
SqlCommand.ExecuteScalar() method
then it would stay
Note: the "type" variable should be defined outside of the using
It's ok just need to change one thing
I would like to contribute one more alternative to the very good answers that this interesting question has had.
If we want to use
ExecuteNonQuery()
, we can use parametersout
, so that SQL Server/Express returns a result to us in said parameter.Then we should include one more parameter to the stored procedure, but specifying that it is an output parameter (
out
):In
SELECT
it we must assign the result of the query to the output parameter:@id_tipo_usuario = ID_TIPO_USUARIO
So the code in VB.NET could be:
As you can see, I put the
Try
englobing toUsing
, because it seems to me that the other way around is wrong.you can put your sp el
SET NOCOUNT OFF
, soexecuteNoQuery()
it returns the number of affected rows