0

I am currently building a Windows Form App project, in which each form needs to have access to the common database. Which of the following method is better:
(1) Build a separate MySqlConnection for each form
(2) Build only one single common MySqlConnection for the entire project. In other words, all the forms share the same MySqlConnection.
My application requires frequent operations on database... I'm afraid that it will ocuupy a bit long time to build the connection every time I am using a new MySqlConnection in using block. Therefore, I wonder if there is a universal standard when the application needs frequent read/write requests on the database.

机灵鬼
  • 1
  • 1
  • 4
    (3) Use a new `MySqlConnection` each time you need to run a query or related group of queries. – madreflection Apr 09 '21 at 15:42
  • It doesn't matter which specific provider you use. As all examples and tutorials show, including Connector/Net tutorials, connections should be short-lived and declared in `using` blocks. Locks are active until a connection closes which means that long-lived connections *increase* blocking and the chance of deadlocks – Panagiotis Kanavos Apr 09 '21 at 15:44
  • *"I'm afraid that it will ocuupy a bit long time to build the connection..."* - There's a connection pool that takes care of that problem. Connection objects and socket connections are not the same thing. The provider (you should use MySqlConnector, not MySql.Data) will map connection objects to pooled socket connections for you. Unless you see some *actual* performance problems that you can objectively trace to establishing connections, you shouldn't be concerned with this. – madreflection Apr 09 '21 at 17:04

0 Answers0