Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/11/2007, 09:01
napael
 
Fecha de Ingreso: noviembre-2007
Mensajes: 1
Antigüedad: 16 años, 7 meses
Puntos: 0
conecta bd mysql con visual .net

hola chicos como estas, les cuento q estoy comenzandoa utilizar mysql conjuntamente con .net, ya q es una exigencia de nuestra facu para realizar una apliccacion, y mi problema es el sig:
* esta es el codigo q tengo
Imports System.Data
Imports System.Data.Odbc
Imports System
Imports System.IO



Public Class formMenuPrincipal
Public conexionBD As OdbcConnection

Private Sub bConectar_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles bConectar.Click
Try
If (txtMotor.Text = "MySQL") Or (txtMotor.Text = "") Then
conexionBD = New OdbcConnection("dsn=localhost" & _
txtODBC.Text & ";uid=MySQL ODBC 3.51 Driver" & _
TxtUsuario.Text & ";pwd=root" & _
txtContrasena.Text & "floresgump")
End If

conexionBD.Open()
lInfo.Text = "Conectado correctamente"
bDesconectar.Enabled = True
bEjecutar.Enabled = True

Catch ex As OdbcException
lInfo.Text = "Error en la conexión"
bDesconectar.Enabled = False
bEjecutar.Enabled = False
MsgBox(ex.Message)
End Try
End Sub

Private Sub bDesconectar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bDesconectar.Click
Try
conexionBD.Close()
lInfo.Text = "Desconectado correctamente"
Catch ex As OdbcException
'silenciosa
lInfo.Text = "Desconectado correctamente"
End Try

End Sub

Private Sub bGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bGuardar.Click
Dim dlAbrir As New _
System.Windows.Forms.SaveFileDialog

dlAbrir.Filter = "Archivos de Texto (*.txt)|*.txt|" & _
"Archivos de SQL (*.sql)|*.sql|" & _
"Todos los archivos (*.*)|*.*"
dlAbrir.CheckFileExists = False
dlAbrir.OverwritePrompt = True
dlAbrir.Title = "Guardar SQL en fichero"
dlAbrir.ShowDialog()
If dlAbrir.FileName <> "" Then
Dim fichero As New _
System.IO.StreamWriter(dlAbrir.FileName)
fichero.WriteLine(txtSQL.Text)
fichero.Close()
End If

End Sub

Private Sub bCargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCargar.Click
Dim dlAbrir As New _
System.Windows.Forms.OpenFileDialog

dlAbrir.Filter = "Archivos de Texto (*.txt)|*.txt|" & _
"Archivos de SQL (*.sql)|*.sql|" & _
"Todos los archivos (*.*)|*.*"
dlAbrir.CheckFileExists = False
dlAbrir.Multiselect = False
dlAbrir.Title = "Abrir fichero SQL"
dlAbrir.ShowDialog()
If dlAbrir.FileName <> "" Then
Dim fichero As New _
System.IO.StreamReader(dlAbrir.FileName)
txtSQL.Text = fichero.ReadToEnd()
fichero.Close()
End If

End Sub

Private Sub bEjecutar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bEjecutar.Click
Try
Dim sql As String = txtSQL.Text
Dim comandoSQL As OdbcCommand = _
New OdbcCommand(sql, conexionBD)
Dim resultadoSQL As OdbcDataReader = _
comandoSQL.ExecuteReader()
Dim i As Integer


txtResultado.Clear()
'Ponemos una fila con el nombre de los campos

For i = 0 To resultadoSQL.FieldCount - 1
If txtResultado.Text <> "" Then
txtResultado.Text = txtResultado.Text & _
Chr(9) & resultadoSQL.GetName(i)
Else
txtResultado.Text = resultadoSQL.GetName(i)
End If
Next i

While resultadoSQL.Read
txtResultado.Text = txtResultado.Text & _
Chr(13) & Chr(10)
For i = 0 To resultadoSQL.FieldCount - 1
If i = 0 Then
txtResultado.Text = txtResultado.Text & _
resultadoSQL(i).ToString
Else
txtResultado.Text = txtResultado.Text & _
Chr(9) & resultadoSQL(i).ToString
End If
Next i
End While

Catch ex As OdbcException
MsgBox(ex.Message)
End Try

End Sub

Private Sub bGuardarResultado_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bGuardarResultado.Click
Dim dlAbrir As New _
System.Windows.Forms.SaveFileDialog

dlAbrir.Filter = "Archivos de texto (*.txt)|*.txt|" & _
"Todos los archivos (*.*)|*.*"
dlAbrir.CheckFileExists = False
dlAbrir.OverwritePrompt = True
dlAbrir.Title = "Guardar resultado ejecución SQL"
dlAbrir.ShowDialog()
If dlAbrir.FileName <> "" Then
Dim fichero As New _
System.IO.StreamWriter(dlAbrir.FileName)
fichero.WriteLine(txtResultado.Text)
fichero.Close()
End If

End Sub

Private Sub bSeleccionarTodo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSeleccionarTodo.Click
txtResultado.SelectAll()

End Sub

Private Sub bCopiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCopiar.Click
txtResultado.Copy()

End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSQL.TextChanged

End Sub
End Class

la generacion es satisfactoria hasta q intento realizar la conexion
ERROR(IM002)(MICROSOFT)(ADMINISTRADOR DE CONTROLADORES ODBC) NO SE ENCUENTRA EL NOMBRE DE ORIGEN Y NO SE ESPECIFICO NINGUN CONTROLADOR PREDETERMINADO.
YO TENGO INSTALADO MYSQL ODBC 3.51 DRIVES PARA Q ME CONECTE LA BASE DE DATOS-
POR FAVOR AYUDENME. GRACIAS