473,769 Members | 3,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# : global connection

RD
I opened a connection to the database in the login form.
Now in another form I tried to insert values to a table.
But the connection is not available in the form.

How can I make the connection global to all forms?
Where I can declare global variables?( Like in VB standard module)

Thanks
RD

Nov 15 '05 #1
4 9346
One of the simplest ways to create a global connection is to create a
class with a static connection, and initialize it when your application
starts:

public class Data
{
public static SqlConnection Connection;
}

Somewhere in startup, you'd have Data.Connection = new
SqlConnection(" ...");

Then you could access it from wherever you need in your project. Using
a connection in this method has some caveats: you need to be careful to open
the connection and close it in an atomic fashion. You don't want two
seperate classes trying to use the connection at the same time, unless you
are very careful to always check the state, but that can quickly become too
complicated. So, if you are going to use a global connection, just be
careful about how you design around it!

Erik

"RD" <di******@allte l.net> wrote in message
news:Fu******** ******@fe01.use netserver.com.. .
I opened a connection to the database in the login form.
Now in another form I tried to insert values to a table.
But the connection is not available in the form.

How can I make the connection global to all forms?
Where I can declare global variables?( Like in VB standard module)

Thanks
RD

Nov 15 '05 #2
Add a public static property or method to a class in your project.
You might want to take a look at this paper about the singleton
pattern:

http://msdn.microsoft.com/practices/...letonincsharp/

--
Scott
http://www.OdeToCode.com

On Tue, 6 Jan 2004 21:51:16 -0500, "RD" <di******@allte l.net> wrote:
I opened a connection to the database in the login form.
Now in another form I tried to insert values to a table.
But the connection is not available in the form.

How can I make the connection global to all forms?
Where I can declare global variables?( Like in VB standard module)

Thanks
RD


Nov 15 '05 #3
Scott Allen <bitmask@[nospam].fred.net> wrote:
Add a public static property or method to a class in your project.
You might want to take a look at this paper about the singleton
pattern:

http://msdn.microsoft.com/practices/...letonincsharp/


Unfortunately, that article is inaccurate. It claims that Double-
Checked Locking is thread-safe in .NET, when it isn't according to
Chris Brumme (who I trust rather more than the author).

See http://www.pobox.com/~skeet/csharp/singleton.html for more options.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Interesting, thanks!

On Thu, 8 Jan 2004 05:47:01 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
Scott Allen <bitmask@[nospam].fred.net> wrote:
Add a public static property or method to a class in your project.
You might want to take a look at this paper about the singleton
pattern:

http://msdn.microsoft.com/practices/...letonincsharp/


Unfortunatel y, that article is inaccurate. It claims that Double-
Checked Locking is thread-safe in .NET, when it isn't according to
Chris Brumme (who I trust rather more than the author).

See http://www.pobox.com/~skeet/csharp/singleton.html for more options.


--
Scott
http://www.OdeToCode.com
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6578
by: Matt Calhoon | last post by:
Hi there, How does IIS 6.0 treat the global.asa file? I had the following problem; 1. uploaded a new site to the win3k server 2. had incorrect db connection string in Session_OnStart in global.asa and website caused errors. 3. Changed the global.asa file to include the correct details. Saved the file. Still got the same errors.
9
1657
by: Mike P | last post by:
I am converting a VB6 app to C# and the VB6 app has 1 connection and 3 recordsets, all global and open all the time so that all the required procedures can use them. Using C#, would it be best to have a global connection and datareaders etc, or is it better to put all the db stuff into a seperate class and make calls to this whenever I need to read/amend data? Any advice would be really appreciated.
6
3697
by: Prince | last post by:
I have a question about the global.asax.cs file. I'm reading info from a database to populate a DataGrid. I read somewhere that the opening of the database should occur in the global.asax.cs file that way every request doesn't open the database again. How is this done? It doesn't seem that the global file can access the DataGrid webcontrol, located on the index.aspx, file. Only the index.aspx.cs file can access this control.
2
3500
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info which each page will use, but my question relates to how to use the database connection i create in all my classes. I have a database class, in a separate namespace and file, i created that handles all the connection opening, executing statements...
2
3530
by: PRTC | last post by:
I'm trying to use the global.asax in my new web aplication proyect using the Application start to store my connection string GLOBAL.ASAX.vb Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim objConnection As OleDbConnection Dim daContent As OleDbDataAdapter Dim objDataReader As OleDbDataReader
12
3828
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it (the oleDBConnection on global.asa.vb) at the other aspx pages ?
35
4850
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection string in the web.config. I created a class with a static database connection and the class opens and closes the database.
3
14761
by: frothpoker | last post by:
Guys, I'm sure this has been asked a million times but I can't seem to formulate a google search that returns what i'm looking for. I've go a dev and live environment. Currently the DB connect string is hard coded into each php file which means i'm going to have to change every page - Arrgh!! If i put the DBonnect into a function and store it in an include file,
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.