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?
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
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
Do add the "End Class" at the end
ASKER
would this be any different to my origianl way??
will it make a difference??
will it make a difference??
ASKER
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?
Postnumber.Text = Class1.noposts2("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
Dim Res As Results = Class1.noposts("General")
Postnumber.Text = Res.Result2 & "/" & Res.Result1
ASKER
Thanks again codecruiser... u saved me again :)
can i asked what the advantages of doing it your way are?
#
is it faster?
can i asked what the advantages of doing it your way are?
#
is it faster?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again :)
Glad to help :-)
you can use it as following
Open in new window
Thanks