client/ClasesModulos/dmModulo.vb

158 lines
6.4 KiB
VB.net

'*****************************
'Noviembre del 2005
'Comentado y modificado por Noé Sánchez
'Importacion de espacios de nombres
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports CLIENTE.Registro 'Clase que encripta y desencripta el password del registro
'Clase que me permite crear la conexion al servidor
Module dmModulo
'Declaracion de variables globales
Public strConnection As String 'Cadena para la conexion del componente.
Public _Usuario As String
Public _Nombre As String
Public _Servidor As String
Public _TipoUsuario As String
Public _DB As String
Public _PassWord As String
Public Conexion As Boolean
Public ConexionCombux As String
Public _Version As String
Public SqlConnection As New SqlConnection()
Public cadenaCon As New propiedades
Sub Main()
'Funcion que permite la conexion a la base de datos
Conexion = False
If Conexion = True Then
Try
SqlConnection.Close()
SqlConnection.ConnectionString = strConnection
SqlConnection.Open() 'Abro la conexion
Conexion = True 'Asigno como verdadero a conexion
Catch dataException As Exception
MsgBox("No se ha podido conectar a la base de datos. Verifique con el administrador del sistema." + Chr(13) + _
"Detalles : " + Chr(13) + dataException.Message, MsgBoxStyle.Information, "Mensaje de sistema")
End Try
'declara una variable de tipo del formulario principal para poder mostrar dicho formulario
'Dim frmPrincipal As Principal2 = New Principal2()
'frmPrincipal.ShowDialog() 'se puede usar solo la propiedad .show
End If
End Sub
Public Function CadenaConexion(ByVal TipoServidor As String, ByVal Servidor As String, _
ByVal BaseDatos As String, ByVal Usuario As String, _
ByVal Password As String) As String
' Esta función devuelve la cadena de conexión para la base de datos...
_Servidor = Servidor
_DB = BaseDatos
Dim DPasswor As String
'Desencripta Password para la conexion
DPasswor = Registro.DesEncripta(Password)
If TipoServidor = "SQL Server" Then
'-- diferentes metodos de conexion
'strConnection = "data source=" & Servidor.Trim & ";initial catalog=" & BaseDatos.Trim & ";password=" & DPasswor.Trim & ";persist security info=True;user id=" & Usuario.Trim & ";workstation id=;packet size=4096"
'strConnection = "data source=" & Servidor.Trim & ";initial catalog=" & BaseDatos.Trim & ";persist security info=FALSE;workstation id=;packet size=4096"
'strConnection = "ODBCPROVIDER;DSN=MINDSCONECCION"
'strConnection = "data source=" & Servidor.Trim & ";initial catalog=" & BaseDatos.Trim & ";password=" & DPasswor.Trim & ";persist security info=True;user id=" & Usuario.Trim & ";workstation id=;packet size=4096"
'strConnection = "data source=" & Servidor.Trim & ";initial catalog=" & BaseDatos.Trim & ";Integrated Security=SSPI;Persist Security Info=False"
'-- conexino definitiva
If Usuario = "" Then
strConnection = " Data Source = " & _Servidor & "; Initial Catalog = " & BaseDatos & "; Integrated Security = True"
Else
strConnection = "server=" & Servidor.Trim & "; uid=" & Usuario.Trim & ";pwd=" & DPasswor & ";database=" & BaseDatos.Trim
End If
End If
clsVaribles.strCon = strConnection
Return (strConnection) 'Devuelve la cadena de conexion
End Function
Public Sub LEE()
Dim StrMFile As System.IO.FileStream
Dim StrMRead As System.IO.StreamReader
Dim strLocation As String = ""
Dim strCtlCadena As String = vbEmpty
Dim strValorRead As String = vbEmpty
Dim StrBasedatos As String = vbEmpty
Dim StrPassword As String = vbEmpty
Dim StrServidor As String = vbEmpty
Dim StrTipoSer As String = vbEmpty
Dim StrUsuario As String = vbEmpty
Dim strFile As String
strLocation = Application.StartupPath & "\Conexion_MindsOP.ini"
strFile = strLocation
If Dir(strFile) <> "" Then
StrMFile = New System.IO.FileStream(strFile, IO.FileMode.Open, IO.FileAccess.Read)
StrMRead = New System.IO.StreamReader(StrMFile)
Do While StrMRead.Peek >= 0
strCtlCadena = StrMRead.ReadLine.ToString
strValorRead = strCtlCadena.Trim.Substring(0, 3).ToUpper
Select Case strValorRead
Case "BAS"
StrBasedatos = BuscaValores(strCtlCadena, "=")
Case "PAS"
StrPassword = BuscaValores(strCtlCadena, "=")
Case "SER"
StrServidor = BuscaValores(strCtlCadena, "=")
Case "TIP"
StrTipoSer = BuscaValores(strCtlCadena, "=")
Case "USU"
StrUsuario = BuscaValores(strCtlCadena, "=")
End Select
Loop
End If
StrMRead.Close()
StrMFile.Close()
dmModulo.CadenaConexion(StrTipoSer, StrServidor, StrBasedatos, StrUsuario, StrPassword)
'If StrUsuario = "" Then
' strConnection = " Data Source = " & StrServidor & "; Initial Catalog = " & StrBasedatos & "; Integrated Security = True"
'Else
' cadenaCon._Cadena = "server=" & StrServidor & "; uid=" & StrUsuario & ";pwd=" & StrPassword & ";database=" & StrBasedatos
'End If
End Sub
Public Function BuscaValores(ByVal Cadena As String, ByVal strDelimited As String) As String
Dim ArrBuscaVal As System.Array
Try
If Cadena <> "" Then
ArrBuscaVal = Cadena.Trim.Split(strDelimited)
BuscaValores = ArrBuscaVal(1)
End If
Catch
MsgBox(Err.Number & ": " & Err.Description & Chr(13) & "Fallo la conexion....Verifique sus propiedades", MsgBoxStyle.Critical)
End Try
End Function
End Module
Public Class propiedades
Private cadena As String
Public Property _Cadena As String
Get
Return cadena
End Get
Set(ByVal value As String)
cadena = value
End Set
End Property
End Class