Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2007, 07:51 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default Global ADO Connection to SQL ?

I am new to using visual basic with SQL, previously I have used Access.
I was wondering if anyone could answer these few questions for me.
If I was to create a login form asking for username and password, and then once that
Was supplied I then connect to my SQL Server using an ado connection, providing I didn’t close the form down (hid it instead), would my connection stay open all the time whilst my user is in my application?
And if I declared the adodb connection as a global variable, whatever form I am in, could I then just use this to call the connection

Ie
Global cnn As New ADODB.Connection

Public Function Connect() As Integer

cnn.Open "Provider=sqloledb;" & _
           "Data Source=SERVERNAME;" & _
           "Initial Catalog=DATABASENAME;" & _
           "User Id=USERID;" & _
           "Password=PASSWORD"
Connect = cnn.State

This function would open my connection when my users first enter my application
And then
Private Sub Command6_Click()
If Connect = 1 Then
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "Select * from Projects"
rs.Open strsql, cnn
    With rs
        Me.projectid = rs.Fields("ProjectId")
        Me.txtname = rs.Fields("name")
        Me.organisation = rs.Fields("organisation")
    End With

rs.Close
Set rs = Nothing
Else
    'Unable to establish a connection
End If
End Sub

Private Sub Form_Close()
cnn.Close
Set cnn = Nothing
End Sub

Private Sub Form_Open(Cancel As Integer)
Call Connect
End Sub
End Function


Is this a good way of doing this rather than opening a new connection everytime I wanted to do something with SQL?

 
Old October 26th, 2007, 03:08 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

It would in fact stay open all the time.

This puts a burden on SQL and the network. If both are lightly loaded, that would be no concern, of course.

But one of the themes to keep in mind to produce the best programs is scalability. This is the idea of how your product will behave as it is called on to be used by a larger user base, and pressed into more service. A program with good scalability withstands this better.

If I were doing what you seem to be doing, I would open a connection at login to verify that the credentials are valid, and then close the connection. When you are going to be hitting the DB again and again, as in a loop that runs one query after another, I would open the connection before the loop, and close it after exiting the loop. This makes for good performance, but minimizes the program's effect overall on databse or network performance.

Don't rely on If Connect = 1 Then. The state of that variable is a snapshot of what was, not an indication of what is. (The server might have had a meteorite land on it since...)
Use If cnn.State = 1 Then or better, avoid "magic number" (the "1", in this case): If cnn.State = adStateOpen Then (if I have that constant right; working from memory here).

I realize that
Code:
    Dim rs As New ADODB.Recordset
    Dim strsql As String

    strsql = "Select * from Projects"
    rs.Open strsql, cnn
    is the typical algorithm you see all over the place, but, since the SQL is used just once, dimming and loading a variable, then using it just once, takes unnecessary time:
Code:
    Dim rs As New ADODB.Recordset
Code:
    rs.Open "Select * from Projects", cnn
    With is not associated with undertaking. So you can make things clearer than
Code:
    rs.Open strsql, cnn
Code:
        With rs
            Me.projectid = rs.Fields("ProjectId") ' You're not using the With...
            Me.txtname = rs.Fields("name")
            Me.organisation = rs.Fields("organisation")
        End With
    rs.Close
    with
Code:
    With rs
        .Open strsql, cnn
        Me.projectid = .Fields("ProjectId")
        Me.txtname = .Fields("name")
        Me.organisation = .Fields("organisation")
        .Close
    End With
    Change Connect to a Sub, and use a routine name that isn't quite such plain English, Like OpenConnection().

Add a CloseConnection Sub if you like, or just use cnn.close.

If you maintain what you have, use parens when receiving the value: If Connect() = adStateOpen Then.

If you switch to using a Sub, then Call Connect would lose the "Call" qualifier.

You really should use more consistent indenting; it will help you as you return to previously developed code, and will make your postings easier to follow. (In this forum, preceding your listing with "code" in square brackets, and ending it in square brackets with a "/" will create fixed-pitch font that will retain the formating that you use in your post. Like this, but without the spaces inside the brackets:

[ code] Dim This As Variable[ /code]
Like this[ code] Dim This As Variable[ /code]period produces:
Like this
Code:
    Dim This As Variable
period

(The line feeds are produced merely by using the [...][/...])

Does that help?
 
Old October 29th, 2007, 03:22 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Yeah that was great, thanks very much for the tips :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
In my Global asax file aDo I need the code below n kenn_rosie VS.NET 2002/2003 0 April 11th, 2006 12:17 AM
Global SQL Connection tallbry VB Databases Basics 2 November 8th, 2004 10:58 PM
ado type library in global.asa -Dman100- Classic ASP Professional 4 August 6th, 2004 09:45 PM
Global SQL connection MichaelTJ .NET Web Services 16 December 16th, 2003 09:34 PM
Global ADO Connection object DaveParry123 ADO.NET 0 October 7th, 2003 03:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.