Link to home
Start Free TrialLog in
Avatar of Member_2_5230414
Member_2_5230414

asked on

Joining two access connections together.

Hey guys,

The below code works but i wanted to know if there was a better way of joining them so i only connect to the db once?

Public Class Class1

    Shared Function noposts(ByVal forumname As String) As String
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
        Dim dbcmd As String = "Select Count(*) From forum where Forum =@forumname"
        Dim cmd As New OleDbCommand(dbcmd, con)
        cmd.Parameters.AddWithValue("@forumname", forumname)
        Try
            con.Open()
            Dim Count As Integer = cmd.ExecuteScalar()
            Return Count
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
    End Function
    Shared Function noposts2(ByVal forumname As String) As String
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
        Dim norep As String = "Select Count(*) From forum where Forum =@forumname and LastPoster <> '' "
        Dim cmd As New OleDbCommand(norep, con)
        cmd.Parameters.AddWithValue("@forumname", forumname)
        cmd.Parameters.AddWithValue("@numrep", "0")


        Try
            con.Open()
            Dim Counter As Integer = cmd.ExecuteScalar()
            Return Counter
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
    End Function
End Class

Open in new window

Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Hi,
you can use it as following
 
Public Class Class1

    Shared Function GetConnection() As OleDbConnection
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
        con.Open()
        Return con
    End Function




    Shared Function noposts(ByVal forumname As String) As String
        Dim con As OleDbConnection = GetConnection()

        Dim dbcmd As String = "Select Count(*) From forum where Forum =@forumname"
        Dim cmd As New OleDbCommand(dbcmd, con)
        cmd.Parameters.AddWithValue("@forumname", forumname)
        Try
            Dim Count As Integer = cmd.ExecuteScalar()
            Return Count
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
    End Function
    Shared Function noposts2(ByVal forumname As String) As String

        Dim con As OleDbConnection = GetConnection()

        Dim norep As String = "Select Count(*) From forum where Forum =@forumname and LastPoster <> '' "
        Dim cmd As New OleDbCommand(norep, con)
        cmd.Parameters.AddWithValue("@forumname", forumname)
        cmd.Parameters.AddWithValue("@numrep", "0")


        Try

            Dim Counter As Integer = cmd.ExecuteScalar()
            Return Counter
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
    End Function
End Class

Open in new window

Thanks
Another approach


Public Class Class1
Public Structure Results
    Dim Result1 As Integer
    Dim Result2 As Integer
End Structure

    Shared Function noposts(ByVal forumname As String) As Results
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\perkinj\My Documents\Visual Studio 2010\WebSites\runningprofiles\forums\forum.mdb;")
        Dim cmd As New OleDbCommand
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@forumname", forumname)
        Try
            con.Open()
            cmd.CommandText = "Select Count(*) From forum where Forum =@forumname"
            Dim Res As Results
            Res.Result1 = cmd.ExecuteScalar()
            cmd.CommandText =  "Select Count(*) From forum where Forum =@forumname and LastPoster <> '' "
            Res.Result2 = cmd.ExecuteScalar()
            
            Return Res
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
    End Function

Open in new window

Do add the "End Class" at the end
Avatar of Member_2_5230414
Member_2_5230414

ASKER

would this be any different to my origianl way??
will it make a difference??
Also before i used

     Postnumber.Text = Class1.noposts2("General") & "/" & Class1.noposts("General")

to display the results ... how would i display the 2 results with your code codecruiser?
Move Results structure to a module

Dim Res As Results = Class1.noposts("General")
Postnumber.Text = Res.Result2 & "/" & Res.Result1
Thanks again codecruiser... u saved me again :)

can i asked what the advantages of doing it your way are?
#
is it faster?
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again :)
Glad to help :-)