|
-
Jan 27th, 2003, 06:59 AM
#1
Thread Starter
Member
Global Db connection
I want to add a global Database connection object to my project. I have created the class and all is working. I have done the following in a form
namespace Forms
{
public class frmLogin : System.Windows.Forms.Form
{
//'DataAccess object
public clsDataAccess gDB = new clsDataAccess();
....
BUT
This teh objects is only accessible from that form. I have to do this on each form that I open in order to access the db.
Is there a a way to create a global object of type clsDataAccess so that I can access the same object from any form or class.
Alternatively, if I just create one object, how can I pass that object to another form??
Any help appreciated.
-
Jan 27th, 2003, 12:18 PM
#2
Let any class (for good design it should not be one of the form classes) contain it as a public static element.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 29th, 2003, 11:05 AM
#3
Thread Starter
Member
Thats Fantastic CornedBee.
It worked!
Cheers.
-
Jul 11th, 2005, 05:51 AM
#4
Addicted Member
Re: Global Db connection
Genie,
Looks like I am almost near to what you have achieved regarding this problem. Can you please post the code into the thread you have raised in vbforums?
If you dont mind can you post the code for the class you have created for global connection and how you have instanciated the connection in the first form using this object.
there r no alternatives 4 hardwork.
-
Jul 11th, 2005, 07:26 AM
#5
Thread Starter
Member
Re: Global Db connection
I don't have that code anymore (been more than 2 years since that post!), but all you need to do is create any class and declare the object as a public static. Then you can access it from anywhere, without even having to instantiate that particular class.
-
Jul 11th, 2005, 07:49 AM
#6
Addicted Member
Re: Global Db connection
Genie,
I have written this code.
Code:
using System;
using System.Configuration;
using System.Data.SqlClient;
public class clsConn
{
public static SqlConnection ConnAA;
public clsConn()
{
//
// TODO: Add constructor logic here
//
ConnAA = new SqlConnection();
ConnAA.ConnectionString = ConfigurationSettings.AppSettings["WlConnString"];
ConnAA.Open();
}
}
Will this be enough to access the connection object
as clsConn.ConnAA? or do I have to written additional lines of code?
Please advise.
there r no alternatives 4 hardwork.
-
Jul 11th, 2005, 07:55 AM
#7
Re: Global Db connection
You should initialize ConnAA to null in its definition, and in the constructor check if it is null and only create the connection object in that case.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 11th, 2005, 08:24 AM
#8
Addicted Member
Re: Global Db connection
Cornedbee,
From what you have told I have altered the code as below.
Code:
using System;
using System.Configuration;
using System.Data.SqlClient;
public class clsConn
{
public static SqlConnection ConnAA = null;
public clsConn()
{
if (ConnAA == null)
{
ConnAA = new SqlConnection();
ConnAA.ConnectionString = ConfigurationSettings.AppSettings["WlConnString"];
ConnAA.Open();
}
}
}
Any more changes required? Please advise.
there r no alternatives 4 hardwork.
-
Jul 11th, 2005, 08:27 AM
#9
Re: Global Db connection
Looks good. In a multi-threaded app, it could cause problems, but that's pretty unlikely.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 11th, 2005, 08:39 AM
#10
Addicted Member
Re: Global Db connection
CornedBee,
Correct me if I am wrong. Do I have to go for a singleton approach to avoid such a problem?
there r no alternatives 4 hardwork.
-
Jul 11th, 2005, 08:49 AM
#11
Re: Global Db connection
This is, in a way, a singleton approach. You're using the connection as a singleton.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|