Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 7th, 2003, 03:44 AM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global ADO Connection object



Hi all,
Not sure if this is specifically an ADO problem, but I have an application which the user logs into SQL server through.

They then manipulate objects at will which relate to the database.
I have the problem that I wish to have a single DB connection for the whole application. In VB6 this was relatively simple - just have a public copy of an ADO connection object in a module.

However C# being object-oriented doesn't allow this (or does it?). Ideally I would like to access the ODBC connection from a single point - eg a static class. Is there a way to make a static variable persist once its class is accessed so that every time I want to save an object in the data access layer back to the DB, a new DB connection isn't created - ie only one for the whole app??

What Im after really is somthing like (pseudo-code)
Code:
namespace DBAccess{
public static class DBConnection
{
private ODBCConnection objConnection;
public static ODBCConnection DBAccess(string username, string password)
{
if(objConnection == null){
objConnection  = new ODBCConnection;
try{
// get user login details
}catch(..)
{}
}
return objConnection; // I want this instance to persist
}
}
}

then I simply want to be able to call the following at any point in the project.
The first call will instantiate the DB connection, and get the user to login.

using DBAccess;
public class someclass1
{
private ODBCConnection objConn = DBConnection.DBAccess;
}

public class someclass2
{
private ODBCConnection objConn = DBConnection.DBAccess;
}
Then the above two classes effectively share the same DB connection
Is this possible???

I've seen one post where the connection string is persisted to a config file - but this is neither secure unless I use some cryptography, and it requires a new connection for every access. Not what I want.

If I'm talking rubbish then please dont be mean - im new to c#.
Thanks
Dave





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
In my Global asax file aDo I need the code below n kenn_rosie VS.NET 2002/2003 0 April 11th, 2006 12:17 AM
Global Connection Object sarahmapg ADO.NET 1 May 18th, 2005 06:00 AM
ADO.Net connection object and InfoMessage event VishalPawar ADO.NET 0 February 23rd, 2005 03:17 AM
ado type library in global.asa -Dman100- Classic ASP Professional 4 August 6th, 2004 09:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.