Code: Detail

VB6 - (Portuguese)

Classe de Gerenciamento de conexao e transacao

Classe responsável por gerenciar conexões e transações.

Last update: 15/10/2015
VB6  VisualBasic6     
 
N/A
N/A
N/A
1
 

Option Explicit

Public Bandeira As String
Dim transacaoAtiva As Boolean
Dim conexaoTransacao As ADODB.Connection

Private Function GetStringConexao() As String
   
   Dim Ambiente As Long
   Ambiente = LerINI("[Environment]")
   
   
               GetStringConexao = "Provider=MSDASQL.1;Password=senha;Persist Security Info=True;User ID=usuario;Data Source=instanciabanco"

End Function

Public Function GetConnection(ByVal band As String) As ADODB.Connection
   
    If Not transacaoAtiva Then
        
        Dim cn As ADODB.Connection
        Set cn = CreateObject("ADODB.CONNECTION")
        
        cn.Open GetStringConexao(band)

        Set GetConnection = cn
    
    Else
        Set GetConnection = conexaoTransacao
    End If
    
End Function

Public Function ExecutarNonQuery(ByVal query As String) As Long
    
    Dim oConexao As ADODB.Connection
    
    Set oConexao = GetConnection(Bandeira)
    
    oConexao.Execute query, ExecutarNonQuery
    
    If Not transacaoAtiva Then Set oConexao = Nothing

End Function

Public Function ExecutarQuery(ByVal query As String) As ADODB.Recordset
    
    Dim rs As ADODB.Recordset
    Dim oConexao As ADODB.Connection
    
    Set rs = CreateObject("ADODB.RECORDSET")
    Set oConexao = GetConnection(Bandeira)
    
    rs.CursorLocation = adUseClient
    rs.Open query, oConexao, adOpenStatic, adLockOptimistic
    
    Set ExecutarQuery = rs
   
    rs.ActiveConnection = Nothing
    Set rs = Nothing
    If Not transacaoAtiva Then Set oConexao = Nothing

End Function

Public Sub InicarTransacao()
    
    Set conexaoTransacao = GetConnection(Bandeira)
    
    conexaoTransacao.BeginTrans
    
    transacaoAtiva = True
    
End Sub

Public Sub CancelarTransacao()
    
    transacaoAtiva = False
    
    conexaoTransacao.RollbackTrans
    
    Set conexaoTransacao = Nothing
    
End Sub

Public Sub ConfirmarTransacao()

    transacaoAtiva = False
    
    conexaoTransacao.CommitTrans
    
    Set conexaoTransacao = Nothing
        
End Sub


'--------------------------------------
'     UTILIZACAO
'--------------------------------------



Public Conexao As DAOConexao


Public Function ConsultarChave(ByVal A As Integer, ByVal B As Long, ByVal C As Integer) As String

   Dim s_sql As String
   Dim rs As New ADODB.Recordset
   
   s_sql = "select CAMPO "
   s_sql = s_sql & "FROM TABELA "
   s_sql = s_sql & "WHERE CAMPO1 = " & CStr(A) & " "
   s_sql = s_sql & "and CAMPO2 = " & CStr(B) & " "
   s_sql = s_sql & "and CAMPO3 = " & CStr(C) & " "
   
   Set rs = Conexao.ExecutarQuery(s_sql)
   
   ConsultarChave = rs!CAMPO

End Function



Public Function CadastrarDadosBasicos(ByVal A As Integer, ByVal B As Integer, ByVal C As Long, ByVal D As Integer) As Long
   
   Dim s_sql As String
   Dim ret As Long

   s_sql = "INSERT INTO TABELA_LOTE_NF(CAMPO1, CAMPO2, CAMPO3,CAMPO4) "
   s_sql = s_sql & "VALUES(" & CStr(A) & " ," & CStr(B) & ", " & CStr(C) & ", " & CStr(D) & ")"
   
   CadastrarDadosBasicos = Conexao.ExecutarNonQuery(s_sql)
   
End Function

Source:
 
Users who have marked this routine as a favorite
 
 
28/10/2015 14:04:38
Muito bom, obrigado por compartilhar

 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.