Results 1 to 7 of 7

Thread: mysql conected to vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    7

    mysql conected to vb.net

    l cant get add data to my data base or delete any .
    l am using mysqlworkbench 5.3 CE , this is what l have done in vb.net


    Code:
    Imports MySql.Data.MySqlClient ' Establece una conexión la librería MySql
    Module Module1
    
    
    
        Public con As New MySqlConnection 'Define la conexiòn
        Private _host As String = "localhost" ' Conecta la base de datos 
        Private _user As String = "root" 'Corresponde al usuario que usted definió cuando crea la BD
        Private _pass As String = "12345" 'Se refiere al password que ustde definíó en la BD
        Public cmd As New MySqlCommand
        Public reader As MySqlDataReader
    
        Sub CONECTAR()
            Try 'Es una cadena de conexión de elementos hacia la BD
                con = New MySqlConnection("Server=" + _host + ";User Id=" + _user + ";Password=" + _pass + ";Database=escuela;")
                con.Open()
                ' If con.State = ConnectionState.Open Then ACTIVE ESTO SI QUIERE COMPROBAR  QUE LA CONEXION ESTA BIEN
                ' MsgBox("Conexión correcta") ' Comprueba que la conexión se ha realizado bien ACTIVE ESTO SI QUIERE COMPROBAR  QUE LA CONEXION ESTA BIEN
                ' End If ACTIVE ESTO SI QUIERE COMPROBAR  QUE LA CONEXION ESTA BIEN
    
            Catch ex As Exception 'POR SI FALLA LA CONEXION
                Console.WriteLine(ex.Message) ' MSG ENVIAR UN ERROR
            End Try
    
        End Sub
    
    
    End Module



    Code:
    Imports MySql.Data.MySqlClient
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Try
    
                CONECTAR()
                Dim Sql As String = "INSERT INTO asignatura(ID_ASIGNATURA,ABREVIATURA, NUMERO,NOMBRE )  VALUES (" + id.Text + " , '" + abrev.Text + "', '" + nom.Text + "', '" + num.Text + "')"
                cmd = New MySqlCommand(Sql, con)
                cmd.ExecuteNonQuery() ' READER AYUDA A RECUPERAR DATOS
                con.Clone()
    
    
            Catch ex As Exception 'POR SI FALLA LA CONEXION
                Console.WriteLine(ex.Message) ' MSG ENVIAR UN ERROR
            End Try
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                CONECTAR()
                Dim Sql As String = "select * FROM asignatura WHERE ID_ASIGNATURA=' id.Text '"
                cmd = New MySqlCommand(Sql, con) ' CON ES EL NOMBRE DE LA CONEXION
                reader = cmd.ExecuteReader() ' READER AYUDA A RECUPERAR DATOS
    
                While reader.Read()
                    id.Text = reader.GetString(0) ' OJO EL TEXT DEBE COINCIDIR CON LA DATA DE LA TABLA
                    abrev.Text = reader.GetString(1) ' OJO EL TEXT DEBE COINCIDIR CON LA DATA DE LA TABLA
                    num.Text = reader.GetString(2) ' OJO EL TEXT DEBE COINCIDIR CON LA DATA DE LA TABLA
                    nom.Text = reader.GetString(3) ' OJO EL TEXT DEBE COINCIDIR CON LA DATA DE LA TABLA
    
                End While
            Catch ex As Exception 'POR SI FALLA LA CONEXION
                Console.WriteLine(ex.Message) ' MSG ENVIAR UN ERROR
            End Try
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            ' CLAUSULA SQL ELIMINAR REGISTRO
            Try
                CONECTAR()
                Dim Sql As String = "DELETE FROM asignatura Where ID_ASIGNATURA= 'id.Text.Trim'"
                cmd = New MySqlCommand(Sql, con)
                Dim myCommand As New MySqlCommand(Sql)
                myCommand.ExecuteNonQuery()
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
    
    End Class

  2. #2
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: mysql conected to vb.net

    i think this wrong in your delete code :
    vb Code:
    1. Dim Sql As String = "DELETE FROM asignatura Where ID_ASIGNATURA= 'id.Text.Trim'"
    should be:

    vb Code:
    1. Dim Sql As String = "DELETE FROM asignatura Where ID_ASIGNATURA= '" & id.Text.Trim  & "'"

    id.Text.Trim is textbox control, doesn't it ?

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: mysql conected to vb.net

    id is the text box.... id.text would be the text contents of the text box... id.text.trim would be a call to the Trim function of a string object that should trim all white space from the front and back of the string.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    7

    Re: mysql conected to vb.net

    l should say first Thanks you .
    yes , l missed the ( & &) , but in the DB ID_ASIGNATURA is Integer type that is why l l dont put "" ""

    now l can insert , add but l cant delete
    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            ' CLAUSULA SQL ELIMINAR REGISTRO
            Try
                CONECTAR()
                Dim Sql As String = "DELETE FROM asignatura Where ID_ASIGNATURA= " & id.Text & ""
                cmd = New MySqlCommand(Sql, con)
                Dim myCommand As New MySqlCommand(Sql)
                myCommand.ExecuteNonQuery()
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
    
        Public Function Sql() As Object
            Throw New NotImplementedException
        End Function
    
    End Class
    something wrong in this , l just dont know what is it ?
    Last edited by lssam; Aug 9th, 2011 at 01:12 PM. Reason: changes in code

  5. #5

    Re: mysql conected to vb.net

    I'm not sure why you have that public function there...that seems like a bad idea. Regardless, what exactly happens?

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    7

    Re: mysql conected to vb.net

    l cant delete from the data base that is all , l want to be able to delete data based on ID . as the code says .

    should l delete that publci funciton ?

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    7

    Re: mysql conected to vb.net

    finally got the reason why :


    here is final code : l had to excute the funciton ExecuteNonQuery() on the calling of the class MySqlCommand after passing to it sql and con arguments .


    Code:
           CONECTAR()
                Dim Sql As String = "DELETE FROM asignatura Where ID_ASIGNATURA = " & id.Text & ""
                cmd = New MySqlCommand
                cmd(Sql, con).ExecuteNonQuery()
                MsgBox("El Registro a sido Eliinado")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width