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




Reply With Quote