本文介绍了我该如何仅使用VB.net在Windows应用程序中创建一次到数据库的连接.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经写了1个模块& 2节课.我已经再次写了与数据库的连接& ;;再次.我希望我只写一次数据库连接.我应该怎么做.
在以下情况下,我在两个我都不想要的类中都调用了SetApplication.我正在使用VB.net使用Windows应用程序

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

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


谢谢

推荐答案


这篇关于我该如何仅使用VB.net在Windows应用程序中创建一次到数据库的连接.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:55