Reputation: 800
Im starting a new project in Visual Studio as Windows Forms Application using C#. Project has login form, main form and other less important forms. All of them uses connection with mysql. Where should i place code responsible for connecting with mysql server to be able to use it in any form? I dont want to create a new connection in every single form. Any hints?
Upvotes: 1
Views: 1373
Reputation:
I would recommend you create a class (call it DataAccess), which would contain all your database access code. You could then reference the namespace of this class in the code for your form and call the appropriate methods to perform CRUD operations.
Upvotes: 0
Reputation: 1499800
I dont want to create a new connection in every single form.
Well, I wouldn't want the UI classes to access the database directly in the first place... but you should be creating a new connection each time you interact with the database, and close it after the operation (probably with a using
statement). Let connection pooling handle the real underlying network connection.
EDIT: See the MySQL connection pooling documentation for more information.
Upvotes: 11