use a public sql connection or create a connection where is needed?

114 Views Asked by At

in a medium application in asp mvc or windows C# which way is betters: 1- create a public sql connection and use in whole of project? 2- create a new sql connection where is needed?

note: 1- in the ado.net or ef. 2- use of transaction usual. thanks for answer.

1

There are 1 best solutions below

0
On

Different application models have different scopes where you should manage the lifetime of a SqlConnection.

For web apps, the current best-practice is to use Request-Scoped connections managed by the Dependency Injection services of the framework.

For Console applications, you'd typically scope each SqlConnection to a using block in some method.

For Windows Forms/WPF apps you could use either local using blocks or scope the SqlConnection to a Form or ViewModel.