-1

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.

asp1984
  • 26
  • 1
  • What is a "public connection"? You better show code of what that looks like instead of using an ambiguous phrase. Even if you know the code is not what you want in the end. – Gert Arnold Aug 15 '21 at 15:24

1 Answers1

0

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.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67