Stored procedures (visual basic.net) and Mysql

0

CLASS CONNECTION

Imports MySql.Data.MySqlClient
Public Class Conexion

Protected cnn As New MySqlConnection

Public idusuario As Integer

Protected Function conectado()
    Try
        cnn = New MySqlConnection("Server=localhost; Uid=root; Database=bdoperador")
        cnn.Open()
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function

Protected Function desconectado()
    Try
        If cnn.State = ConnectionState.Open Then
            cnn.Close()
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function
End Class

CLASS FCLIENTE

Public Class fcliente
Inherits Conexion
Dim cmd As New MySqlCommand

Public Function mostrar() As DataTable
    Try
        conectado()

        cmd = New MySqlCommand("mostrar_cliente")
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cnn



        If cmd.ExecuteNonQuery Then
            Dim dt As New DataTable
            Dim da As New MySqlDataAdapter(cmd)
            da.Fill(dt)
            Return dt
        Else
            Return Nothing
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
        Return Nothing
    Finally
        desconectado()
    End Try
  End Function
  End Class

My stored procedure is show_client

CREATE PROCEDURE mostrar_cliente()
BEGIN
SELECT * FROM cliente ORDER BY idcliente desc;
END

If I do it that way I get an error in the mysql Now if I believe it that way

CREATE PROCEDURE mostrar_cliente()
SELECT * FROM cliente ORDER BY idcliente desc;

I do not get an error but nothing happens in the form but if I make a select only for example

SELECT * FROM cliente ORDER BY idcliente desc

there if you upload the data to the form I'd like to know why it does not load the data when I do a procedure.

    
asked by user107978 22.11.2018 в 14:46
source

1 answer

1

In the part that says:

If cmd.ExecuteNonQuery Then

Change it to:

If cmd.ExecuteReader Then

The ExecuteNonQuery is used when you do not want to return results, for example UPDATE, INSERT, and DELETE as opposed to ExecuteReader that returns a set of records depending on the query.

I hope it's useful, Regards.

    
answered by 26.11.2018 в 05:06