Avatar of Declan Basile
Declan Basile
Flag for United States of America

asked on 

Should I have one Global or multiple Module level SQL Connection objects?

I'm writing a vb.net application that connects to a SQL Server database.  Should a create one SQL Connection object at the main form's class level, assign it a connection string and open it in the load of the main form, keep the connection open for the duration of the program and use it whenever there's a SQL Command object in any of the modules that needs an open SQL Connection object?  Or should I only have module level SQL Connection objects, and every time there's a command object in a module that needs a SQL connection object I would declare a local SQL connection object (local to the module), assign it the connection string, and open it specifically for SQL commands in that module?  In the first approach there will always be a connection the SQL Server even when the program is idle and there doesn't need to be a connection; however, in the second approach the program would have to keep instantiating, assigning a connection string, opening and closing a SQL connection every single time it needs one.  Which is better?
Microsoft SQL ServerVisual Basic.NETSQL

Avatar of undefined
Last Comment
Declan Basile

8/22/2022 - Mon