Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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")
           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 19-Sep-12 3:24am
Edited 19-Sep-12 3:26am
Wes Aday94.3K
v2

1 solution

Rate this: bad
good
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) {
            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)
  Permalink  
Comments
neldesai at 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 at 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 at 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 at 19-Sep-12 9:17am
   
There is a tip/trick here[^] that you can refer to
neldesai at 19-Sep-12 9:25am
   
thankx
Andrei Straut at 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
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2014
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