Idea needed: how to connect to DB in one place?

eramgarden

Well-known member
Joined
Mar 8, 2004
Messages
579
Ok, my first C# WinForm ..

I have a menu: File --> connect to Database

So we want to connect to a database in one place, once in the beginning of the app ..Users enter the database name, userid and password...

So, how should I do this? Write a "connection" class and pass the parameters to the class? Do the connection in the code behind of the form when the user clicks "connect" button??

what is the best way of doing this?
 
I usually keep one internal sealed class that encapsulates all of the objects and code that the application needs to interact with the data source....I would suggest having a class similar to this in which you store the connection string you build from the users input in a private static field. Then have a public static GetConnection() method that returns a connection when needed. There are obviously many other things to consider in regards to security, exception handling/recovery, etc...but that is the basic gist of one way to handle data connections.

eramgarden said:
Ok, my first C# WinForm ..

I have a menu: File --> connect to Database

So we want to connect to a database in one place, once in the beginning of the app ..Users enter the database name, userid and password...

So, how should I do this? Write a "connection" class and pass the parameters to the class? Do the connection in the code behind of the form when the user clicks "connect" button??

what is the best way of doing this?
 
Last edited by a moderator:
Back
Top