You are on page 1of 14

Integrantes:

Collantes Piguave Carlos


Murillo Láinez Jonathan

PASOS PARA LA CONECCION CON ODBC

1. Colocamos el nombre que utilizaremos y en servidor de igual manera el


nombre de nuestro servidor y le damos clic en siguiente

2. Damos clic en siguiente Si tiene usuario y clave el servidor habilitamos con autentificación
de SQL Server e ingresamos los datos como es usuario y contraseña, para esta práctica
dejaremos con autentificación de Windows y dar clic en siguiente.
3. En la siguiente pantalla habilitamos establecer con la siguiente base de datos como
predeterminada y seleccionamos la base con la que queremos conectar.

4. Damos clic en siguiente y finalizar


CODIGO FRM_PERFILES PARA LA CONECCION CON ODBC

Imports System.IO

Public Class frm_perfiles


Dim pos_Codigo As Integer = 0
Dim pos_Nombre As Integer = 1
Dim pos_Descripcion As Integer = 2
Dim pos_estado As Integer = 3

Private Sub frm_perfiles_Load(sender As Object, e As EventArgs) Handles


MyBase.Load
If fun_leerdatos() = True Then
MsgBox("Se conectó con éxito")
Else
MsgBox("Se NO pudo conectar")
MsgBox("Error, Comunicar al proveedor...")
Me.Close()
End If
Try
If fun_cargarPerfiles() = False Then
MsgBox("no existen perfiles")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Function fun_cargarPerfiles() As Boolean


Try
fun_cargarPerfiles = False
Dim str_cadenaSql As String = ""
str_cadenaSql = str_cadenaSql & " select top (10)
per_Id,per_nombre,per_Descripcion,per_estado from seg_Perfil "
str_cadenaSql = str_cadenaSql & " where per_Nombre like '" &
txt_buscar.Text & "%'"
If fun_conectar() = False Then
Exit Function
End If
dr = fun_ExecuteReader(str_cadenaSql)
dgv_datos.Rows.Clear()
If dr.HasRows Then
While dr.Read
dgv_datos.Rows.Add(dr("per_Id"), dr("per_nombre"),
dr("per_Descripcion"), dr("per_estado"))
End While

fun_cargarPerfiles = True
End If

Catch ex As Exception
fun_cargarPerfiles = False
Finally
dr.Close()
fun_desconectar()
End Try
End Function

Private Sub dgv_datos_CellDoubleClick(sender As Object, e As


DataGridViewCellEventArgs) Handles dgv_datos.CellDoubleClick
Try
If e.RowIndex >= 0 Then
Dim frm As New frm_matPerfiles
frm.txt_codigo.Text =
dgv_datos.Rows(e.RowIndex).Cells(pos_Codigo).Value
frm.ShowDialog()
If fun_cargarPerfiles() = False Then
MsgBox("no existen perfiles")

End If

End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub txt_buscar_TextChanged(sender As Object, e As EventArgs)


Handles txt_buscar.TextChanged
Try
fun_cargarPerfiles()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub btn_nuevo_Click(sender As Object, e As EventArgs) Handles


btn_nuevo.Click
Try

Dim frm As New frm_matPerfiles


frm.txt_codigo.Text = 0
frm.chk_estado.Checked = True

frm.ShowDialog()

If fun_cargarPerfiles() = False Then


MsgBox("no existen perfiles")

End If

Catch ex As Exception
MsgBox(ex.Message)

End Try
End Sub

Private Sub btn_cancelar_Click(sender As Object, e As EventArgs) Handles


btn_cancelar.Click
Me.Close()
End Sub
Function fun_leerdatos() As Boolean
Try
fun_leerdatos = False
Dim directorio, cadena As String
Dim SERV As String
directorio = System.AppDomain.CurrentDomain.BaseDirectory() &
"ARCHIVOCONFIGURACION.txt"
Dim objReader As New StreamReader(directorio)
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
cadena = ""
For Each sLine In arrText
cadena = cadena & sLine
Next
Dim arr = cadena.Split(":")
SERV = arr(1) 'servidor
g_str_cadena_conec = "DSN=" & SERV
fun_leerdatos = True
Catch ex As Exception
fun_leerdatos = False
End Try

End Function
End Class

--CODIGO MODIFICADO EN MOD_ACCESODATOS PARA CONECCION DE ODBC

Imports System.Data.Odbc

Module mod_AccesoDatos
'coneccion con sql
'Public g_str_conexion As String
'Dim cnn As SqlConnection
'Dim cmd As SqlCommand
'Public dr As SqlDataReader

'coneccion con ODBC


Public cnn As OdbcConnection
Dim cmd As OdbcCommand
Public dr As OdbcDataReader

Public Function fun_conectar() As Boolean


Try
fun_conectar = False
cnn = New OdbcConnection
cnn.ConnectionString = g_str_cadena_conec
If cnn.State = ConnectionState.Closed Then
cnn.Open()
fun_conectar = True
End If

Catch ex As Exception
fun_conectar = False
End Try
End Function
Public Function fun_desconectar() As Boolean
Try
fun_desconectar = False
If cnn.State = ConnectionState.Open Then
cnn.Close()
fun_desconectar = True
End If
Catch ex As Exception
fun_desconectar = False
End Try
End Function
Public Function fun_ExecuteReader(ByVal cadenaObdc As String, Optional i As
Integer = 0) As OdbcDataReader

Try
cmd = New OdbcCommand
cmd.CommandText = cadenaObdc
If i = 0 Then
cmd.CommandType = CommandType.Text
Else
cmd.CommandType = CommandType.StoredProcedure
End If
cmd.Connection = cnn
Return cmd.ExecuteReader()
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function

Public Function fun_ExecuteReader(ByVal cadenaodbc As String, ByVal


ParamArray parametro() As String) As OdbcDataReader
Try
Dim i As Integer
cmd = New OdbcCommand
cmd.CommandText = cadenaodbc
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
'cargar los parámetros
OdbcCommandBuilder.DeriveParameters(cmd) 'trae los parámetros
For i = 1 To parametro.Length
CType(cmd.Parameters(i), OdbcParameter).Value = parametro(i - 1)
Next
Return cmd.ExecuteReader()
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
Public Function fun_ExecuteNonQuery(ByVal cadena As String) As Boolean
Try
'graba o actualiza pasando una cadena de texto
cmd = New OdbcCommand
cmd.CommandText = cadena
cmd.CommandType = CommandType.Text
cmd.Connection = cnn
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
MsgBox(ex.Message)

Return False
End Try
End Function

Public Function fun_ExecuteNonQuery(ByVal procedimiento As String, ByVal


ParamArray parametro() As String) As Boolean
Dim i As Integer
Try
'graba o actualiza
cmd = New OdbcCommand
cmd.CommandText = procedimiento
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
'cargar los parametros
OdbcCommandBuilder.DeriveParameters(cmd) 'trae los parametros
For i = 1 To parametro.Length
CType(cmd.Parameters(i), OdbcParameter).Value = parametro(i - 1)
Next
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Public Function fun_iniciatransaccion() As Boolean
Try
fun_iniciatransaccion = False
Dim cadenasql As String
cadenasql = "BEGIN TRAN miproceso"
If fun_ExecuteNonQuery(cadenasql) = True Then
fun_iniciatransaccion = True
Else
MsgBox("No se Pudo Iniciar la Transacci�n")
fun_iniciatransaccion = False
End If
Catch ex As Exception
fun_iniciatransaccion = False
End Try
End Function
Public Function fun_commit() As Boolean
Try
fun_commit = False
Dim cadenasql As String
cadenasql = "COMMIT TRAN miproceso"
If fun_ExecuteNonQuery(cadenasql) = True Then
fun_commit = True
End If
Catch ex As Exception
fun_commit = False
End Try
End Function
Public Function fun_rolbak() As Boolean
Try
fun_rolbak = False
Dim cadenasql As String
cadenasql = "ROLLBACK TRAN miproceso"
If fun_ExecuteNonQuery(cadenasql) = True Then
fun_rolbak = True
End If
Catch ex As Exception
fun_rolbak = False
End Try
End Function

End Module

En el MOD_GENERAL añadimos la siguiente línea de código.


--MOD_GENERAL

Public g_str_cadena_conec As String


INGRESO A LA TABLA FRM_PERFILES

2. Corremos el programa y vemos que es correcto ya que nos muestra el


mensaje de “Se conecto con éxito”
3. Nos ingresa a nuestra tabla perfiles y le damos a ingresar nuevo.

4. Ingresamos un nuevo perfil y guardamos y vemos que nos dice que la


operación se realizó con éxito
5. Verificamos nuestra tabla perfiles y vemos que efectivamente se guardo
nuestro nuevo perfil.

You might also like