Hola yo realice dicha clase en Vb .Net. Lo primero que hice fue que en la capa presentación crear un archivo de configuración que lo puedes obtener desde agregar elementos
app.config
Código XML:
Ver original<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="CadenaConexion" connectionString="server=localhost;User Id=root;password=;database=control_vehicular"
providerName="MySql.Data.MySqlClient" />
<add name="PRESENTACION.My.MySettings.ConnectionString" connectionString="Dsn=CadenaConexion;uid=root"
providerName="System.Data.Odbc" />
</connectionStrings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
Ahora en la Capa Acceso de Datos. Creo la clase:
ConexionAD:
Código vb:
Ver original'Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Imports System.Configuration
Public Class ConexionAD
Private Conexion As String
Public Sub New()
Try
Conexion = ConfigurationManager.ConnectionStrings("CadenaConexion").ConnectionString.ToString()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Function abrir() As MySqlConnection 'SqlConnection
'Dim cCon as SqlConnection =New SqlConnection(CadenaConexion)
'cCon="CadenaConexion="+ CadenaConexion + ""
'conexion=New SqlConnection(cCon)
Dim cCon As New MySqlConnection(Conexion) 'SqlConnection(Conexion)
Try
cCon.Open()
Return cCon
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source)
End Try
Return cCon
End Function
End Class
Y en la misma capa creo otra clase denominada:
ConductorAD:
Código vb:
Ver original'Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Imports System.Data.SqlTypes
Imports ENTIDAD
Public Class ConductorAD
Private conn As New MySqlConnection 'SqlConnection
Private conn1 As New MySqlConnection 'SqlCommand
Private comando As New MySqlCommand 'SqlCommand
#Region "Cadena de conexion"
Public Sub New()
Dim Objconexion As New ConexionAD
conn = Objconexion.abrir
comando.Connection = conn
End Sub
#End Region
#Region "Función convertir datos"
Private Shared Function Convertirdatos(ByVal reader As IDataReader) As ConductorEN
Dim conductor As New ConductorEN
conductor.idcond = LTrim(RTrim(Convert.ToInt32(reader(0))))
conductor.dni = LTrim(RTrim(Convert.ToString(reader(1))))
conductor.nomconductor = LTrim(RTrim(Convert.ToString(reader(2))))
conductor.apconductor = LTrim(RTrim(Convert.ToString(reader(3))))
conductor.departamento = LTrim(RTrim(Convert.ToString(reader(4))))
conductor.ciudad = LTrim(RTrim(Convert.ToString(reader(5))))
conductor.direccion = LTrim(RTrim(Convert.ToString(reader(6))))
conductor.tipolicencia = LTrim(RTrim(Convert.ToString(reader(7))))
conductor.fechacadulic = LTrim(RTrim(Convert.ToDateTime(reader(8))))
conductor.fechanac = LTrim(RTrim(Convert.ToDateTime(reader(9))))
conductor.telefono = LTrim(RTrim(Convert.ToString(reader(10))))
conductor.correo = LTrim(RTrim(Convert.ToString(reader(11))))
If Not IsDBNull(reader("foto")) Then
'If Not IsDBNull(reader(12)) Then
conductor.foto = DirectCast(reader("foto"), Byte())
End If
Return conductor
End Function
#End Region
#Region "Función listar"
Public Function listaconductor() As List(Of ConductorEN)
Dim list As New List(Of ConductorEN)
Dim reader As MySqlDataReader 'SqlDataReader
Try
comando.CommandType = CommandType.StoredProcedure
comando.CommandText = "ListaConductor"
reader = comando.ExecuteReader
While reader.Read
list.Add(Convertirdatos(reader))
End While
comando.Parameters.Clear()
Return list
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source)
Throw New Exception("Error: ConductorAD-->Listado() " + ex.Message, ex)
Finally
comando.Parameters.Clear()
conn.Close()
conn.ClearAllPools()
conn = Nothing
End Try
End Function
#End Region
End Class
Ojala te pueda haber ayudado.