Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have written 1 module & 2 Classes . I have written connection to database again & again. I want that i should write Database Connection only once . How i should do.
In the below case i have called SetApplication in both the classes which i don't want . I am using Windows Application using VB.net
VB
Module subMain
    Public Sub Main()

        Dim clsa As clsa
        clsa = clsa

        Dim clsb As clsb
        clsb = New clsb

        ' Starting the Application
        System.Windows.Forms.Application.Run()

    End Sub
End Module


Friend Class ClsA
    Private WithEvents SBO_Application As SAPbouiCOM.Application
     Dim SboGuiApi = New SAPbouiCOM.SboGuiApi
    Dim oForm As SAPbouiCOM.Form
    
    Private Sub SetApplication()
        
        Try
            Dim sConnectionString As String
            sConnectionString = Environment.GetCommandLineArgs.GetValue(1)
            SboGuiApi.Connect(sConnectionString)
            SBO_Application = SboGuiApi.GetApplication()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub New()
        MyBase.New()
        Try
            SetApplication()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Friend Class ClsB
    Private WithEvents SBO_Application As SAPbouiCOM.Application
    Dim SboGuiApi = New SAPbouiCOM.SboGuiApi
    Dim oForm As SAPbouiCOM.Form
    Private Sub SetApplication()
        
        Try
            Dim sConnectionString As String
            sConnectionString = Environment.GetCommandLineArgs.GetValue(1)
            SboGuiApi.Connect(sConnectionString)
            SBO_Application = SboGuiApi.GetApplication()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub New()
        MyBase.New()
        Try
            SetApplication()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Thanks
Posted
Updated 10-Jun-12 5:49am
v2

1 solution

The answer is very simple - don't do it! Database connections are cached in .NET, and what looks to you as destroying old connections and creating new ones is actually dealing with just one and the same connection, just different instances of connection wrappers, but that's no burden for your application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900