Click here to Skip to main content
13,347,480 members (52,407 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have write this code for module:

Module Module1
Public Class cn
       Public Shared con As New SqlConnection

       Public Shared Sub openconnection()
           con = New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=PROJ;Integrated Security=True")
       End Sub

       Public Shared Sub close()

       End Sub
   End Class
end module

my problem is i want to used this code for opening and closing connection that i used for multiple forms.
Can this code is work and how can i call this methods or used this codes.That is my main aim is to minimized codes and code re usability. how can i achieved this.
i am using 2010 and back end sql server 2008. There is any alternative for this types of implementation.
waiting for some help..
Posted 19-Sep-12 3:24am
Updated 19-Sep-12 3:26am
Wes Aday104.2K

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Having multiple forms, you could make a singleton class that wraps around your connection. Then when calling your singleton instance, check if that connection is already open, and if it is, you don't open it again.

As you seem to be in luck today (I don't usually post code, but I am doing this kind of work as we speak, although I don't need to keep connections open across forms), I'll post you some stuff with basic comments:

class DatabaseServiceOracle {
    private static DatabaseServiceOracle instance;
    private static OracleConnection connection;

    //Empty private constructor, we don't want multiple instances of this class
    private DatabaseServiceOracle() {}

    /*Use this to acquire a reference to present class, and make sure 
    that reference never points to a null object*/
    public static DatabaseServiceOracle getInstance() {
    	if(instance == null) {
            instance = new DatabaseServiceOracle();
    	return instance;

    public OracleConnection getConnection() {
        if(connection == null) {
            //Init your connection here

        /*We don't normally open connections when acquiring them, but we
        do it on a per-need basis*/
        if(connection.State != ConnectionState.Open) {

        return connection;

Also, don't mind the formatting, although the code is C#, we have an internal convention that says we use the Java formatting conventions even when we do C#.

Also, the database system is Oracle and not SQL Server, but the point stands.

Furthermore, this is conceived for an MVC system, so in here would go most of the DB queries needed (it's just a basic template that we customize for our needs)
neldesai 19-Sep-12 8:48am
thank you so much...can i get this type of information to learn on internet..can you give me some sites if there is available..
Andrei Straut 19-Sep-12 8:52am
Yes, just google "Singleton".

Most of this concepts I've learned in college, and then experience came while working. I have no links to provide you with, other than Google itself, which will most definitely help you each time you have a problem.

EDIT: There are also A LOT of well-written beginner articles here on CP, just look around and you'll find most of what you'll ever need
neldesai 19-Sep-12 9:04am
thank you and can you give little bit help..
in i can't declare static variable as you declare in this code so how can i solve this problem because i am not using c#.
Andrei Straut 19-Sep-12 9:17am
There is a tip/trick here[^] that you can refer to
neldesai 19-Sep-12 9:25am
Andrei Straut 19-Sep-12 9:41am
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100