Search code examples
c#.netasp.net-mvc-4usingsqlclient

Reducing number of using in C#


My question might be silly or may not be a question at all, but here it goes..

I am performing quite some database operations in my ASP.net MVC project where I am creating two objects each and every time namely, SqlConnection and SqlCommand.

I have shown an example below

using (SqlConnection connection = new SqlConnection(connectionString))
{
  using (SqlCommand command = new SqlCommand("sRegisterUser", connection))
  {

I am doing all these different operations inside one class under different methods.

My question is how can I reduce the creation of these objects each and every time? How can I create them globally

PS: sRegisterUser is a stored procedure, like wise other methods use different procedures which gets different values as parameters.

Please help me out.

Thanks for any help in advance.


Solution

  • The answer is, don't. You do not want to share these objects, you are using them appropriately.