Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database access from everywhere in application

Tags:

c#

oop

database

If I wanted to access a database in Delphi, I could add a datamodule to a project, configure it from my mainform and then access it anywhere in the application; a reference would be stored in a global variable.

I know that in C# and other more modern OO languages global variables are frowned upon. So how can I access my database from where I need it? The biggest problem I have is the configuration: location, user, password, etc. are unknown at design time.

I now have a db-class and make a new instance when I need it, but then I would have to store those settings in some globally accessible thing, and I have simply moved the problem.

What's the standard solution?

Thanks, regards, Miel.

like image 732
Miel Avatar asked Feb 14 '26 07:02

Miel


1 Answers

I always use the singleton pattern. As for configuration, look at the System.Configuration.ConfigurationManager class which allows you to read settings from your project's app.config/web.config file.

like image 128
David Brown Avatar answered Feb 15 '26 23:02

David Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!