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 29th, 2003, 02:16 PM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global SQL Connection

Hi

Im a newbie to vb.net but am developing an windows application that uses several windows forms. rather that keep on opening a new connection string each time i open the form, i want to be able to use a global connection class and then create an instance of that class each time i open a window/form - does anyone know how i can go about it

Thanks

A newbie vb.net developer

 
Old November 8th, 2004, 10:50 PM
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi tallbry

I am also an upcomming vb .net superstar, Contrary to popular beleif it is possible to do as you wish, although many have said that keeping an open connection is a bad idea, however this is not as bad as it is made out to be, and can be less costly on the sql server than opening a connection each time you need to retrieve data.

so here is how to do it in vb .net

create a vb .net windows application

add a module to the application, adding the following code

Imports System.Data.SqlClient
Module Module1
    Public Success As String
    Public connection As SqlConnection

    Sub main()
        Dim Database As String = "[databasename]"
        Dim ServerName As String = "[servername]"
        Dim ConString As String = "Server=" & ServerName & ";Initial Catalog=" & Database & ";User ID=;Password="
        Dim connection As New SqlConnection(ConString)
        ' connect to the db
        Try
            connection.Open()
        Catch
            'errormessage
            Success = "sql connection failed"
        End Try
        Success = "sql connection successful"
        Dim form1 As New Form1
        form1.ShowDialog()
    End Sub
End Module

of course changeing the sql connection requirements to suit your environment.

you will now be able to call these variables from any form within your application.

on form1 add the following code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox(Success)
    End Sub

this will generate a message box that will tell you if the connection has failed or succeded.

from here all you need to do when you require data is call connection this connection will remain available untill you exit the application.
 
Old November 8th, 2004, 10:58 PM
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Appologies tallbry

In my haist to help you out i forgot to mention that you will also need to go into the project properties and change that startup object to sub main.

p.s the most important part in sub main is "form1.ShowDialog()"
without this the application will automatically close once it completes the sub routine (sub main)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Global ADO Connection to SQL ? debbiecoates VB Databases Basics 2 October 29th, 2007 03:22 AM
How to create a Global Database connection? chrislepingwell ADO.NET 6 November 8th, 2006 07:20 PM
Global Connection Object sarahmapg ADO.NET 1 May 18th, 2005 06:00 AM
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.