sql-server – How to save data from a query in a stored procedure from VisualBasic.Net?

Question:

I need to create a Login with mandatory user types with stored procedures in SQL Server. I have not managed to capture the user type id.

User types:

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 runs without errors, but I don't know how to capture the query log

Answer:

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 obtain the result of the query, using for example ExecuteScalar() , which will directly return ID_TIPO_USUARIO .

Or if you want to get the full result, you should use ExecuteReader() .

Scroll to Top