Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have write this code for module:

VB
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")
           con.Open()
       End Sub

       Public Shared Sub close()

           con.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 vb.net 2010 and back end sql server 2008. There is any alternative for this types of implementation.
waiting for some help..
Posted
Updated 19-Sep-12 2:26am
v2

1 solution

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:

C#
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) {
            connection.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)
 
Share this answer
 
Comments
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 vb.net 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    
thankx

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900