Esta clase sirve para conectarse a una Base de Datos de SQL. Es necesario agregar en las referencias del projecto las dll: System, System.Data, System.XML.
Imports System.Data
Imports System.Data.SqlClient
Public Class clsConexion
Public Function FG_ConnectionString_Get(ByVal PP_STR_Servidor As String, ByVal PP_STR_BaseDatos As String, ByVal PP_STR_User_Id As String, ByVal PP_STR_Password As String) As String
Dim VP_STR_ConnectionString As String
VP_STR_ConnectionString = "Data source=" + PP_STR_Servidor + ";"
VP_STR_ConnectionString = VP_STR_ConnectionString + "initial catalog=" + PP_STR_BaseDatos + ";"
VP_STR_ConnectionString = VP_STR_ConnectionString + "user id=" + PP_STR_User_Id + ";"
VP_STR_ConnectionString = VP_STR_ConnectionString + "password=" + PP_STR_Password + ""
'Regresa el Resultado
FG_ConnectionString_Get = VP_STR_ConnectionString
End Function
Public Function FG_SqlConnection_Open(ByVal VP_STR_ConnectionString As String) As SqlConnection
Dim VP_SQL_Conexion As SqlConnection
'Crea la conexion de la BD.
VP_SQL_Conexion = New SqlConnection(VP_STR_ConnectionString)
'Regresa el Resultado
FG_SqlConnection_Open = VP_SQL_Conexion
End Function
Public Sub PG_SqlCommand_Init(ByRef PP_SQL_Conexion As SqlConnection, ByRef PP_SQL_Comando As SqlCommand, ByVal PP_STR_Nombre_SP As String)
With PP_SQL_Comando
.Connection = PP_SQL_Conexion
.CommandText = PP_STR_Nombre_SP
.CommandType = CommandType.StoredProcedure
.Parameters.Clear()
End With
'Abre la Conexion.
PP_SQL_Conexion.Open()
End Sub
Public Sub PG_Comando_Parameter_Set_Integer(ByRef PP_SQL_Comando As SqlCommand, ByVal PP_STR_Nombre_Parametro As String, ByVal PP_INT_Valor As Integer)
'Configura un Parametro de Tipo Entero
Dim VP_PAR_Parametro As New SqlParameter
With VP_PAR_Parametro
.ParameterName = (PP_STR_Nombre_Parametro)
.SqlDbType = SqlDbType.Int
.Value = PP_INT_Valor
End With
'Asigna el Parametro
PP_SQL_Comando.Parameters.Add(VP_PAR_Parametro)
End Sub
Public Sub PG_Comando_Parameter_Set_Char(ByRef PP_SQL_Comando As SqlCommand, ByVal PP_STR_Nombre_Parametro As String, ByVal PP_STR_Valor As String)
'Configura un Parametro de Tipo Char
Dim VP_PAR_Parametro As New SqlParameter
With VP_PAR_Parametro
.ParameterName = (PP_STR_Nombre_Parametro)
.SqlDbType = SqlDbType.Char
.Value = Trim(PP_STR_Valor)
End With
'Asigna el Parametro
PP_SQL_Comando.Parameters.Add(VP_PAR_Parametro)
End Sub
Public Sub PG_Comando_Parameter_Set_Decimal(ByRef PP_SQL_Comando As SqlCommand, ByVal PP_STR_Nombre_Parametro As String, ByVal PP_DEC_Valor As Decimal)
'Configura un Parametro de Tipo Decimal.
Dim VP_PAR_Parametro As New SqlParameter
With VP_PAR_Parametro
.ParameterName = (PP_STR_Nombre_Parametro)
.SqlDbType = SqlDbType.Decimal
.Value = PP_DEC_Valor
End With
'Asigna el Parametro
PP_SQL_Comando.Parameters.Add(VP_PAR_Parametro)
End Sub
Public Sub PG_Comando_Parameter_Set_Date(ByRef PP_SQL_Comando As SqlCommand, ByVal PP_STR_Nombre_Parametro As String, ByVal PP_DAT_Valor As Date)
'Configura un Parametro de Tipo Fecha.
Dim VP_PAR_Parametro As New SqlParameter
With VP_PAR_Parametro
.ParameterName = (PP_STR_Nombre_Parametro)
.SqlDbType = SqlDbType.DateTime
.Value = PP_DAT_Valor
End With
'Asigna el Parametro
PP_SQL_Comando.Parameters.Add(VP_PAR_Parametro)
End Sub
Public Sub PG_Comando_ExecuteNonQuery(ByVal PP_SQL_Comando As SqlCommand)
PP_SQL_Comando.ExecuteNonQuery()
End Sub
Public Sub PG_DataTable_Fill(ByVal PP_SQL_Comando As SqlCommand, ByRef PP_DTA_Resultado As DataTable)
'Llena el DataTable.
Dim VP_DAP_Resultado As New SqlDataAdapter
VP_DAP_Resultado.SelectCommand = PP_SQL_Comando
VP_DAP_Resultado.Fill(PP_DTA_Resultado)
End Sub
Public Sub PG_DataSet_Fill(ByVal PP_SQL_Comando As SqlCommand, ByRef PP_DTS_Resultado As DataSet)
'Llena el DataTable.
Dim VP_DAP_Resultado As New SqlDataAdapter
VP_DAP_Resultado.SelectCommand = PP_SQL_Comando
VP_DAP_Resultado.Fill(PP_DTS_Resultado)
End Sub
Public Sub PG_SqlConnection_Close(ByRef PP_SQL_Conexion As SqlConnection)
If PP_SQL_Conexion.State = ConnectionState.Open Then
PP_SQL_Conexion.Close()
End If
End Sub
End Class
Filed under: Visual Studio | Deja un Comentario »